tags:

views:

257

answers:

1

I am trying to display an amount of colorpicker controls depending on an amount of colors in a xaml resourcedictionary file.

For some reason I can't figure out the right way to do this. When loading it in through a XAMLReader to a ResourcesDictionary Object, I m not sure what is the best way to iterate over it.

I had first tried to handle it as xml, using XDocument.Elements() which gave an empty IEnumerable when trying to get al the elements.

What is the best way to do this ?

example of the xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

    <!-- Edit the FontFamily  value to change the application font-->
    <FontFamily x:Key="NormalFontFamily">Arial</FontFamily>

    <!-- Edit ARGB values (hex) to change the colours used in the application -->
    <Color x:Key="NormalForegroundColor" A="0xFF" R="0xFF" G="0xFF" B="0xFF" />
    <Color x:Key="NormalForegroundColor80" A="0xFF" R="0xB6" G="0xB5" B="0xB5" />
    <Color x:Key="DarkerForegroundColor" A="0xFF" R="0x97" G="0x97" B="0x97" />
    <Color x:Key="DarkestForegroundColor" A="0xFF" R="0x76" G="0x76" B="0x76" />
    <Color x:Key="NormalBackgroundColor" A="0xFF" R="0x22" G="0x22" B="0x22" />
    <Color x:Key="DarkerBackgroundColor" A="0xFF" R="0x19" G="0x19" B="0x19" />
    <Color x:Key="LighterBackgroundColor" A="0xFF" R="0x33" G="0x33" B="0x33" />
     ....
+1  A: 

Is there a reason why you can't use your colors from a resource dictionary directly like in the example shown below? For simplicity I am showing "inline" resources instead of a separate resource dictionary file. Why do you need to load your resource dictionary in code?

<Window.Resources>
    <x:Array x:Key="colors" Type="{x:Type Color}">
        <x:Static Member="Colors.White" />
        <x:Static Member="Colors.Red" />
        <x:Static Member="Colors.Green" />
        <x:Static Member="Colors.Blue" />
    </x:Array>
</Window.Resources>

<ListBox ItemsSource="{StaticResource colors}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Rectangle Width="20" Height="20" Margin="2"
                        Stroke="Black">
                    <Rectangle.Fill>
                        <SolidColorBrush Color="{Binding}" />
                    </Rectangle.Fill>
                </Rectangle>
                <TextBlock Text="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

If you really must enumerate resources in code then I suggest you reference your resource file in your XAML and use FrameworkElement.FindResource in code-behind to get a hold of your data. In particular example from above, code might look something like:

 var colors = (IEnumerable) FindResource("colors");
 foreach(Color color in colors)
 {
    // Do something here...
 }
wpfwannabe
Thank you.I want to be able to at runtime import new themes(=resourcedictionary), and be able to edit them aswel. So I do not want to compile in my resourcefiles.
Sdry
If you want to enumerate a `ResourceDictionary` at runtime, your `XamlReader.Load` approach will work well. Try this: using (var stream = new FileStream(@"path to your file", FileMode.Open)) { var resourceDictionary = (ResourceDictionary) XamlReader.Load(stream); var colors = (IEnumerable) resourceDictionary["colors"]; foreach(Color color in colors) { // ... } }
wpfwannabe
Hmm, how would you get separate items, not as part of a collection in xaml ? Have editted in an xaml example in my questionIts just that I am still wondering, in terms of my project, I can probably implement it you suggested.
Sdry
Well, you can do something like this: foreach(Color color in resourceDictionary) { ... }. `ResourceDictionary` is just an `IDictionary` as well as `IEnumerable` so you can iterate over it however you like. In the code above you could extract all `Color` s from the dictionary in your original question.
wpfwannabe
In what way could I then find the key of that color ?
Sdry
You have to be more specific in your requests. To get keys you do this: foreach(var key in resourceDictionary.Keys) { var color = resourceDictionary[key] as Color; }
wpfwannabe
foreach (System.Collections.DictionaryEntry de in ResourceDictionary) { if (de.Value is Color) { Console.WriteLine("####--->" + de.Key + " <----> " + ((Color)de.Value)); } }This is what i was looking for, sorry for the not being specific enough, your answer and comments have helped me a lot. First time using a non generic dictionary.
Sdry