Change OnClick label color WPF

Total Post:183


 6394  View(s)
Rate this:

Im coming from a C# winforms background and I would normally do all this in code. I have several Labels that I'm using as a menu. When the mouse hovers over them the text changes color by:


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

    <SolidColorBrush x:Key="mouseOverColor" Color="Gold" />

    <Style TargetType="Label" x:Key="menuItemStyle">


            <Trigger Property="IsMouseOver" Value="False">

                <Setter Property="Foreground" Value="{StaticResource normalColor}"/>


            <Trigger Property="IsMouseOver" Value="True">

                <Setter Property="Foreground" Value="{StaticResource mouseOverColor}"/>





      <Label x:Name="Label_Video1" Style="{StaticResource menuItemStyle}" Content="1.Video 1."  FontSize="16" HorizontalAlignment="Left" Margin="25,74,0,0" VerticalAlignment="Top" MouseLeftButtonDown="Label_Video1_MouseLeftButtonDown" />

    <Label x:Name="Label_Video2" Style="{StaticResource menuItemStyle}" Content="2. Video 2." FontSize="16" HorizontalAlignment="Left" Margin="25,105,0,0" VerticalAlignment="Top" MouseDown="Label_Video2_MouseDown"/>

When the user clicks a label I want it to stay a certin color (In this case Gold) and all the others to stay their normal colour. So if a label has been previously clicked and I click another label it will go from gold to white etc

  1. Re: Change OnClick label color WPF

    Hi Ankita,

    When using WPF, you have to think slightly differently. We know that the Label control doesn't know when it has been clicked and it especially doesn't know when another Label element has been clicked... but some controls do. Thinking about it... a RadioButton has exactly this behaviour. Now this is where WPF really shines.

    We can use RadioButtons and by providing them with a new ControlTemplate, we can make them look like plain text

    <Grid Background="Black">
            <Style TargetType="{x:Type RadioButton}">
                <Setter Property="Template">
                        <ControlTemplate TargetType="{x:Type RadioButton}">
                            <TextBlock Text="{TemplateBinding Content}">
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Foreground" Value="White" />
    <DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType={
    x:Type RadioButton}}}" Value="True">
        <Setter Property="Foreground" Value="Gold" />
    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource
    AncestorType={x:Type RadioButton}}}" Value="True">
        <Setter Property="Foreground" Value="Gold" />
        <StackPanel Margin="5" Background="{x:Null}">
            <RadioButton Content="1.Video 1." />
            <RadioButton Content="2.Video 2." />
            <RadioButton Content="3.Video 3." />

    If you don't want the RadioButtons to be all together in a StackPanel, then you can use give them the same GroupName property value to ensure that they still operate as one group.

      Modified On Apr-03-2018 06:29:15 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.