C# 如何对ItemsControl中的每个项目应用不同的颜色集?

C# 如何对ItemsControl中的每个项目应用不同的颜色集?,c#,wpf,colors,itemscontrol,C#,Wpf,Colors,Itemscontrol,我有一个ItemsControl,希望每个项目根据其包含的数据设置其颜色主题。我有两个资源字典,用于两个可能的主题(红色和蓝色)和一个数据模板,它定义了如何应用这些颜色。如何为每行分配当前资源字典 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schem

我有一个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布局代码,同时仍然能够在其上有两种不同的颜色样式。

与您的任务相关,您可以应用
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

你有四个选择在你的一次性

  • 将您的
    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>
    
    代码:

        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) });
        }
    

  • 有一个替代计数:

    <ItemsControl  ItemsSource="{Binding List}"
                      AlternationCount="2"
                      HorizontalContentAlignment="Stretch">
    
    
    
    加上:

    
    
    是否要根据正在模板化的数据的某些属性指定颜色?索引?完全是别的吗?是的,项目包含一个可以转换为样式名的属性。我不理解某些部分。您正在编写示例,就好像我有多个数据模板一样。我只有1个数据模板和2套画笔。我试图避免重复DataTemplate布局代码,因为我实际项目中的代码相当大。@Poma我建议这样做的方法。因为你想做的是不可能的。如果没有代码,条件Res字典加载是不可能的。仔细看我的最后一部分,或者更新你的问题。
        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) });
        }
    
    <ItemsControl  ItemsSource="{Binding List}"
                      AlternationCount="2"
                      HorizontalContentAlignment="Stretch">
    
    <DataTemplate.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="White" TargetName="Grid" />
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#FFF1F1F1" TargetName="Grid" />
        </Trigger>
    </DataTemplate.Triggers>