Hi Guys,
Me again looking for some more articals or tutorials on Static Resources and binding them through XAML.
Kind regards,
Kieran
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hemant KumarRank #1,202 · 1.5K reputationPosted Oct 30, 2011, 8:00 AM EDT
you can get resources object from your code using FindResource method or Resources property of the FrameworkElement. Check if code below would work for you:
// find resource object //string[] resource = (string[])FindResource("AllColors"); string[] resource = (string[])Resources["AllColors"]; if (resource != null) { // old values foreach (var item in resource) Console.WriteLine(item); // set new values Resources["AllColors"] = new string[] { "red", "blue", "green"}; } // find resource and show its new values resource = (string[])Resources["AllColors"]; if (resource != null) foreach (var item in resource) Console.WriteLine(item);though I don't think it's going to work the way you want it or it's a correct solution for your task. What you can do is define a class with colors collection:
public class TestColors { public TestColors() { Colors = new ObservableCollection(); Colors.Add("red"); Colors.Add("blue"); Colors.Add("green"); } public ObservableCollection Colors { get; set; } }in your xaml you can define an ObjectDataProvider of the given object type and bind your control to its Colors property. Smth like this:
... xmlns:local="clr-namespace:YourNamespace" ... Window.Resources> ObjectDataProvider x:Key="AllColors0" ObjectType="{x:Type local:TestColors}"/> Window.Resources> ... ListBox ... DataContext="{StaticResource AllColors0}" ItemsSource="{Binding Colors}"/> ...another way of how you can do it is binding your controls to a property of the window, smth like this:
public partial class MainWindow : Window { public static string[] TestProperty { get { List result = new List(); result.Add("red"); result.Add("green"); result.Add("blue"); return result.ToArray(); } } public MainWindow() { InitializeComponent(); ...xaml:
ListBox ... ItemsSource="{x:Static local:MainWindow.TestProperty}"/>update0: define resource in the code
// define resource in the code Resources["AllColors1"] = new string[] { "red1", "blue1", "green1" }; // show new values resource = (string[])Resources["AllColors1"]; if (resource != null) foreach (var item in resource) Console.WriteLine(item);hope this helps, regards