【问题标题】:DataTrigger with a custom color具有自定义颜色的 DataTrigger
【发布时间】:2021-04-18 08:55:37
【问题描述】:

我正在尝试根据另一个控件的颜色(即Border)设置LabelForeground 颜色。 如果我为边框使用预定义的 XAML 颜色,它可以工作,但如果我使用自定义 RGB 颜色,它就不行。

以下示例有效,因为用于边框的颜色是blue

<Application.Resources>
    <Style x:Key="labelStyle" TargetType="Label">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding BorderBrush, ElementName=borderControl}" Value="blue">
                <Setter Property="Foreground" Value="red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Application.Resources>
<Border x:Name="borderControl" Background="#ffecec" BorderBrush="blue" BorderThickness="1" Padding="10" Margin="20" CornerRadius="5">
    <Label Style="{StaticResource labelStyle}" Content="This message is red if border color is blue" />
</Border>

以下示例不起作用,因为用于边框的颜色是#f5aca6

<Application.Resources>
    <Style x:Key="labelStyle" TargetType="Label">
        <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding BorderBrush, ElementName=borderControl}" Value="#f5aca6">
                <Setter Property="Foreground" Value="red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Application.Resources>
<Border x:Name="borderControl" Background="#ffecec" BorderBrush="#f5aca6" BorderThickness="1" Padding="10" Margin="20" CornerRadius="5">
    <Label Style="{StaticResource labelStyle}" Content="This message is green and should be red" />
</Border>

【问题讨论】:

    标签: c# wpf xaml datatrigger


    【解决方案1】:

    问题是您分配给Brush 类型属性的颜色(如BorderBrush)将通过类型转换器自动转换为画笔instance。对于像#f5aca6 这样的纯色,将在幕后创建SolidColorBrush 的实例。如果您使用Blue 之类的预定义画笔之一,您实际上是在访问Brushes 类上的静态实例

    现在为什么它适用于预定义的画笔而不适用于自定义颜色?画笔类型不会覆盖相等比较器,因此DataTrigger通过引用比较它们。预定义的画笔是静态实例,因此它始终是相同的引用。但是,自定义颜色将创建一个画笔的新实例,因此它们不等于通过引用预定义的画笔。

    您可以通过在 XAML 中创建自定义颜色的画笔实例并使用它来解决此问题。

    <SolidColorBrush x:Key="CustomBorderBrush" Color="#f5aca6" />
       
    <Style x:Key="labelStyle" TargetType="Label">
       <Setter Property="Foreground" Value="Green" />
       <Style.Triggers>
          <DataTrigger Binding="{Binding BorderBrush, ElementName=borderControl}" Value="{StaticResource CustomBorderBrush}">
             <Setter Property="Foreground" Value="red" />
          </DataTrigger>
       </Style.Triggers>
    </Style>
    
    <Border x:Name="borderControl" Background="#ffecec" BorderBrush="{StaticResource CustomBorderBrush}" BorderThickness="1" Padding="10" Margin="20" CornerRadius="5">
       <Label Style="{StaticResource labelStyle}"Content="This message is green and should be red" />
    </Border>
    

    在具有纯色的特殊情况下,您也可以比较样式中画笔的Color 属性,但请注意,这对于其他画笔类型(如GradientBrush)会失败。

    <Style x:Key="labelStyle" TargetType="Label">
       <Setter Property="Foreground" Value="Green" />
       <Style.Triggers>
          <DataTrigger Binding="{Binding BorderBrush.Color, ElementName=borderControl}" Value="#f5aca6">
             <Setter Property="Foreground" Value="red" />
          </DataTrigger>
       </Style.Triggers>
    </Style>
    

    【讨论】:

    • 感谢您的出色回答并改进了我的问题,因为英语不是我的第一语言。
    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多