带有自定义颜色的 DataTrigger

问题描述

我正在尝试根据另一个控件(Foreground)的颜色设置 LabelBorder 颜色。 如果我为边框使用预定义的 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>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)