2010-04-26 36 views
1

我想顯示一定數量的colorpicker控件,具體取決於xaml resourcedictionary文件中的顏色數量。迭代resourcedictionary xaml文件

由於某種原因,我找不出正確的方法來做到這一點。當通過XAMLReader將其加載到ResourcesDictionary對象時,我不確定什麼是迭代它的最佳方式。

我第一次嘗試將它作爲xml處理,使用XDocument.Elements()在嘗試獲取元素時給出了一個空的IEnumerable。

這樣做的最好方法是什麼? 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" /> 
    .... 

回答

1

是否有一個理由,爲什麼你不能在下面的例子中使用的顏色從資源字典直接樣的?爲了簡單起見,我顯示「內聯」資源而不是單獨的資源字典文件。爲什麼你需要在代碼中加載資源字典?

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

如果你真的必須枚舉代碼資源那麼我建議你引用您的資源文件在你的XAML並使用代碼隱藏FrameworkElement.FindResource,讓您的數據保持。在上面的例子中,代碼可能看起來像這樣:

var colors = (IEnumerable) FindResource("colors"); 
foreach(Color color in colors) 
{ 
    // Do something here... 
} 
+0

謝謝。 我希望能夠在運行時導入新主題(= resourcedictionary),並能夠編輯它們aswel。所以我不想在我的資源文件中編譯。 – 2010-04-28 09:10:29

+1

如果你想在運行時枚舉一個'ResourceDictionary',你的'XamlReader.Load'方法可以很好地工作。試試這個: 使用(var stream = new FileStream(@「path to your file」,FileMode.Open)) {0} {0} var resourceDictionary =(ResourceDictionary)XamlReader.Load(stream); var colors =(IEnumerable)resourceDictionary [「colors」]; 的foreach(Colour彩色的顏色) { // ... }} 嗯 – wpfwannabe 2010-04-28 09:43:24

+0

,你怎麼會得到單獨的項目,而不是在XAML集合的一部分?在我的問題中編輯了一個xaml示例 它只是我仍然想知道,就我的項目而言,我可以實現它,你可以建議。 – 2010-04-29 08:51:33