user10649740
user10649740

Reputation:

Do you have a solution which makes Foreground works like Background?

Please run following code and put mouse cursor on TextBlock in order to see what following code is doing.

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBlock Foreground="Blue" Text="www.google.com" Height="20" Width="100">
        <TextBlock.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="TextBlock.IsMouseOver" Value="True">
                        <Setter Property="TextBlock.Background" Value="Red"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</Grid>
</Window>

Then replace above code from

<Setter Property="TextBlock.Background" Value="Red"/>

to

<Setter Property="TextBlock.Foreground" Value="Red"/>

and see that Foreground is not working.

Do you have a solution which makes Foreground works like Background?

Upvotes: 3

Views: 64

Answers (1)

vasily.sib
vasily.sib

Reputation: 4002

The reason it is not working is that you set Foreground="Blue" explicitly on your TextBlock. This will override any value from style triggers. Change your XAML like this:

<TextBlock Text="www.google.com" Height="20" Width="100">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Blue"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Upvotes: 4

Related Questions