2016-03-04 33 views
0

我有一個ItemsControl,並希望每個項目根據其包含的數據設置其顏色主題。我有2個可用主題(紅色和藍色)的資源字典和一個定義如何應用這些顏色的DataTemplate。如何爲每一行分配當前資源字典?如何將不同的顏色設置應用於ItemsControl中的每個項目?

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication1" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <!-- 2 dictionaries with style colors --> 
      <ResourceDictionary x:Key="RedStyle"> 
       <SolidColorBrush x:Key="BorderBrush" Color="Red" /> 
       <SolidColorBrush x:Key="TextBrush" Color="Red" /> 
      </ResourceDictionary> 

      <ResourceDictionary x:Key="BlueStyle"> 
       <SolidColorBrush x:Key="BorderBrush" Color="Blue" /> 
       <SolidColorBrush x:Key="TextBrush" Color="Blue" /> 
      </ResourceDictionary> 

     </ResourceDictionary> 
    </Window.Resources> 
    <Grid> 
     <ItemsControl ItemsSource="{Binding list}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Border BorderBrush="{StaticResource BorderBrush}"> 
         <TextBlock Text="{Binding}" Foreground="TextBrush" /> 
        </Border> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl>   
    </Grid> 
</Window> 

更新:的DataTemplate中和套刷是我真正的項目大得多。我想要做的是避免重複DataTemplate佈局代碼,同時仍然能夠擁有2種不同的顏色樣式。

+0

你想分配基於數據的一些屬性正在被模板的顏色?索引?還有其他的東西嗎? –

+0

是的項目包含一個屬性,可以轉換成樣式名稱 – Poma

回答

0

相關的你的任務,你可以申請DataTriggers像顯示在下面的例子:

<ItemsControl.Style> 
    <Style> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Items.TheAttribute}" Value="AttributeValue1"> 
       <Setter Property="BorderBrush" Value="Red"/> 
       <Setter Property="TextBrush" Value="Red"/> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding Items.TheAttribute}" Value="AttributeValue2"> 
       <Setter Property="BorderBrush" Value="Blue"/> 
       <Setter Property="TextBrush" Value="Blue"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</ItemsControl.Style> 

在這個例子中,你並不需要在你的ResourceDictionary指定的Styles

或者,您可以使用<Style.Triggers>根據您的ResourceDictionary設置Style,而不是設置各個屬性的值。

希望這可能有所幫助。

0

注意:無法使用Triggers有條件地應用ResourceDictionary。

你有一次性使用四種選擇。

  1. 把你DataTemplate在單個資源字典,或者在不同的字典相應DataTemplate秒。

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
    
    <DataTemplate DataType="{x:Type sys:String}"> 
        <TextBlock Text="{Binding .}"> 
         <TextBlock.Style> 
          <Style TargetType="TextBlock"> 
           <Style.Triggers> 
            <Trigger Property="Text" Value="Name1"> 
             <Setter Property="Background" Value="Red"/> 
            </Trigger> 
           </Style.Triggers> 
          </Style> 
         </TextBlock.Style> 
        </TextBlock> 
    </DataTemplate> 
    
    </ResourceDictionary> 
    

    用法:

    <ItemsControl.Resources>    
          <ResourceDictionary Source="RedStyle.xaml"/> 
        </ItemsControl.Resources>    
    
  2. 使用DataTemplateSelector應用一些複雜的邏輯,並分配相應DataTemplates。在這個網上有好的tutorials

  3. 使用混合行爲來使用純XAML來做到這一點。

  4. 使用Loaded事件控制出現在DataTemplate使用代碼加載Res字典。最簡單的方法!

     <DataTemplate> 
          <TextBlock Loaded="TextBlock_Loaded" Text="{Binding .}"/> 
         </DataTemplate> 
    

    代碼:

    private void TextBlock_Loaded(object sender, RoutedEventArgs e) 
        { 
         TextBlock tb = sender as TextBlock; 
         if (tb.Text == "Name123") 
          tb.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("RedStyle.xaml", UriKind.Relative) }); 
        } 
    
+0

我不明白一些部​​分。您正在編寫示例,就好像我有多個DataTemplates一樣。我只有1個DataTemplate和2組畫筆。我試圖避免重複DataTemplate佈局代碼,因爲我的真實項目中的一個比較大。 – Poma

+0

@Poma我建議的方法。因爲你試圖做的事情是不可能的。沒有代碼的條件Res字典加載是不可能的。仔細查看我的最後一部分,或更新您的問題。 – AnjumSKhan

相關問題