我使用资源字典添加一个绿色方块

时间:2012-12-03 16:06:45

标签: wpf dictionary resources runtime tabitem

我已经为我的tabcontrol添加了一个资源字典供我使用。每个tabitem在右侧有一个小框,在资源字典中定义。

                               

我想根据每个tabitem中的复选框更改颜色。目前,如果我在代码中执行此操作,则所有tabitems都会更改颜色

LinearGradientBrush lgbrush =(LinearGradientBrush)(this.FindResource(“TabItemSideBackground”));                     lgbrush.GradientStops [0] .Color = Colors.AntiqueWhite;                     lgbrush.GradientStops [1] .Color = Colors.Red;                     lgbrush.GradientStops [2] .Color = Colors.OrangeRed;

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我会选择触发器。在TabItem DataTemplate或样式中,您可以添加处理属性上特定值的触发器,并执行属性设置。请查看MSDN上的DataTrigger课程页面。

简短示例

<Style TargetType="TabItem">
    <Style.Triggers>
      <DataTrigger Binding="{Binding ElementName=SomeObject, Path=SomeProperty}" Value="MakeItRed">
        <Setter Property="Foreground" Value="{DynamicResource MyRedBrush}" />
      </DataTrigger>    
    </Style.Triggers>
</Style>
相关问题