Harry Boy
Harry Boy

Reputation: 4777

Change default Hover over text color wpf

I have used to following to change my hover over text color in my labels. But the default text color is black. How do I modify the below have the default color to be something else e.g white.

    <Page.Resources>
    <SolidColorBrush x:Key="mouseOverColor" Color="Gold" />
    <Style x:Key="mouseOverStyle" TargetType="Label">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="{StaticResource mouseOverColor}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

Upvotes: 2

Views: 6622

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81313

You don't need to override template at all. This can be achieved with following style only:

<SolidColorBrush x:Key="mouseOverColor" Color="Gold" />
<Style TargetType="Label" x:Key="mouseOverStyle">
   <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Foreground" Value="{StaticResource mouseOverColor}"/>
       </Trigger>
   </Style.Triggers>
</Style>

Your trigger won't work in case you are specifying default value of Foreground on your label.

Won't work for this label:

<Label Content="sjdfjfddjfjdfs"
       Style="{StaticResource mouseOverStyle}" Foreground="Green"/>

However, will work for this label:

<Label Content="sjdfjfddjfjdfs" Style="{StaticResource mouseOverStyle}"/>

Even your style will work but make sure you are not setting local value for Foreground DP. This is because of Dependency Property Precedence order where local value holds higher precedence than setter values of style.

Upvotes: 6

Chris
Chris

Reputation: 8656

Have you tried changing the Value for the Foreground property in your Setter?

e.g.

<Setter Property="Foreground" Value="White" />

Or defining a Brush of your desired color and using that? e.g.

<SolidColorBrush x:Key="myColor" Color="White" />

<!-- Your Existing Code -->
    <Setter Property="Foreground" Value="{StaticResource myColor}" />
<!-- Your Existing Code -->

Rohit's answer is much simpler/more sensible if you're just after the label color change (rather than doing anything with the ControlTemplate.

Upvotes: 1

Related Questions