开发者

iterating resourcedictionary xaml file

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">

    <!-- 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" />
     ....


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...
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜