Reputation: 3259
I have this button in XAML:
<Button Style="{StaticResource HeadButton}">
<TextBlock Style="{StaticResource HeadButtonText}">Add flavor</TextBlock>
</Button>
And I need that when you hover over the button, the TextBlock
Foreground property should change (I know I can use Content in the button but I'm doing it like this for another reason).
How can I achieve this?
Upvotes: 1
Views: 1810
Reputation: 2875
You can bind to the foreground property of the button, and use triggers to modify it.
<Style x:Key="Test" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
<Button Style="{StaticResource Test}">
<TextBlock Text="Click HERe" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}}"/>
</Button>
Upvotes: 1
Reputation: 435
Here is a code example of what you're trying to achieve. Although may I note it's not best practice to place a TextBlock
directly inside the button as the text field is already there, and for the TextBlock
to use the text field rather than writing between the tags like HTML
<Button Name="btn">
<TextBlock Text="Add Flavor">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=btn, Path=IsMouseOver}" Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Button>
Upvotes: 1