Change text color on mouse over in WPF using VB.NET

In this article, we will see how to change the text color on mouse over in WPF.
  • 5901
We will use 'SolidColorBrush' and 'Style' in  'Window.Resources' And ''Label use in 'Grid'. We will use SolidColorBrush as a Resource.

ControlTemplate:-We will use 'ControlTemplate' in the style. This 'ControlTemplate' change the color in your text.  We will use a 'ControlTemplate' to control the colors, and TextBlocks don't support ControlTemplates. The ControlTemplate change the foreground color of text when the user mouse over text.

Example:-

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <SolidColorBrush x:Key="mouseOverColor"
                    Color="Pink" />
        <Style x:Key="myStyle" 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>
    </Window.Resources>
    <Grid>
        <Label HorizontalAlignment="Center"
          VerticalAlignment="Center"
          Content="The Change Text Color"
          FontSize="20"
          FontWeight="bold"
          Style="{StaticResource myStyle}" />
    </Grid>
</
Window>

Output:- After run the application show image this type.


 textcolor1.bmp

After the mouse enter on the label and change the text color. The text color show in the pink color.

textcolor2.bmp

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.