Question

Im pretty new to coding and struggling with this for the last few hours and can't even find the proper way to do it.

I want to load all the data from one ResourceDictionary Skin.xaml in DataGrid and display it on screen so the user can then change the content and save it back to the same skin or another one.

this is part of the data from the ResourceDictionary xaml

<SolidColorBrush x:Key="Button01" Color="#FFABABAB"/>
<SolidColorBrush x:Key="TitleBar" Color="#FFABABAB"/>
<SolidColorBrush x:Key="Background" Color="#FF5B5B5B"/>

I alredy created the DataGrid and binded a DataTable to it to insert the data

this.ResourceToLoad = new DataTable("Resources");
        DataColumn workCol = ResourceToLoad.Columns.Add("Name", typeof(String));
        workCol.AllowDBNull = false;
        workCol.Unique = true;
        this.ResourceToLoad.Columns.Add("Color", typeof(String));
        dgResources.DataContext = this;

The problem I have is how to insert the data from the ResourceDictionary to the DataTable.

Was it helpful?

Solution

If the ResourceDictionary is loaded in your context you can do the following to load a resource:

var buttonBrush = (Brush)FindResource("Button01");

Otherwise you have to first load the ResourceDictionary:

ResourceDictionary loadedDictionary;
using (FileStream fs = new FileStream("yourpath/Skin.xaml", FileMode.Open))
    {
       loadedDictionary = (ResourceDictionary)XamlReader.Load(fs);
    }

var buttonBrush = (Brush)loadedDictionary["Button01"];

Edit: With a foreach loop you could get key and value of the dictionary entry:

 foreach (DictionaryEntry entry in loadedDictionary)
 {
    var key = entry.Key;
    var resource = entry.Value;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top