Reputation: 387
I´m trying to set the Foreground
color of a Label
based on the color of the other control which is a Border
.
If I use a predefined XAML color for the border it works, but if I use a custom RGB color, it does not.
The following example works, because the color used for the border is 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>
The following example does NOT work, because the color used for the border is #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>
Upvotes: 0
Views: 732
Reputation: 22089
The issue is that the color that you assign to property of type Brush
like BorderBrush
will automatically be converted into a brush instance by a type converter. For solid colors like #f5aca6
an instance of a SolidColorBrush
will be created behind the scenes. If you use one of the predefined brushes like Blue
, you are actually accessing static instances on the Brushes
class.
Now why does it work for predefined brushes and not for custom colors? The brush types do not override equality comparers, so the DataTrigger
will compare them by reference. Predefined brushes are static instances, so it is always the same reference. However, custom colors will create a new instance of a brush, therefore they are not equal to the predefined brushes by reference.
You can solve this issue by creating a brush instance of your custom color in XAML and using that.
<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>
In the special case of having a solid color, you could alternatively compare the Color
property of the brush in the style, but be aware that this fails for other brush types like 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>
Upvotes: 2