3
votes

I got this TextBlock:

<TextBlock Foreground="Red"/>

And there is an implicit style for TextBlock with style trigger that asks "if the foreground is {StaticResource BrushTextBlockAlertForeground} then set the background to black." (BrushTextBlockAlertForeground is Red, of course).

<Trigger Property="Foreground" Value="{StaticResource BrushTextBlockAlertForeground}">
    <Setter Property="Background" Value="Black"/>
</Trigger>

This trigger condition fails! If static resource resolved on loading, so why this trigger fails? Shouldn't the XAML loader put there Red in the trigger condition? or it puts some expression instead? Is there any chance that happening because the "Value" property of trigger condition isn't dependency property?

Only when I write

<Trigger Property="Foreground" Value="Red">
    <Setter Property="Background" Value="Black"/>
</Trigger>

It works.

If I put static resource from outside (look below), it doesn't work in any case. Like that:

<TextBlock Foreground="{StaticResource BrushTextBlockAlertForeground}"/>

I would love to know the reason behind, because I want to write reusable color, instead of putting "red" in many places. "Tomorrow" someone will try to make that reusable and will encounter the behavior I experiencing.

1
Can't reproduce the issue. It doesn't show in the designer for me but when I run the app, Background is changed to Black. - XAMlMAX
you will work if the value inside textbox and trigger are the same type - cuongtd
@Rise It's not enough, that they would be from the same type, but they should be the same object. - Rekshino

1 Answers

1
votes

Ensure, TextBlocks you test and StyleTrigger use both(!) the same brush Red or from StaticResource. TextBlock with foreground Red and StyleTrigger with StaticResource and vice versa will not work, because the values Brushes.Red and from StaticResource aren't equal. See A: How to Compare SolidColorBrushes?

<StackPanel>
            <!--this doesn't work-->
            <StackPanel.Resources>
                <SolidColorBrush x:Key="forebr" Color="Red"/>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="{StaticResource forebr}">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="Red" Text=" Test trigger 0"></TextBlock>
        </StackPanel>

        <StackPanel>
            <!--this doesn't work-->
            <StackPanel.Resources>
                <SolidColorBrush x:Key="forebr" Color="Red"/>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="Red">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="{StaticResource forebr}" Text=" Test trigger 1"></TextBlock>
        </StackPanel>

        <StackPanel>
            <!--this works-->
            <StackPanel.Resources>
                <SolidColorBrush x:Key="forebr" Color="Red"/>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="{StaticResource forebr}">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="{StaticResource forebr}" Text=" Test trigger 2"></TextBlock>
        </StackPanel>

        <StackPanel>
            <!--this works-->
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Foreground" Value="Red">
                            <Setter Property="Background" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="Red" Text=" Test trigger 3"></TextBlock>
        </StackPanel>