---
title: "Change OnClick label color WPF"  
description: "Change OnClick label color WPF"  
author: "Anonymous User"  
published: 2013-12-15  
updated: 2013-12-16  
canonical: https://www.mindstick.com/forum/1772/change-onclick-label-color-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# Change OnClick label color WPF

Im coming from a C# winforms [background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp) and I would normally do all this in code. I have several [Labels](https://www.mindstick.com/forum/34568/badges-labels-page-headers) that I'm using as a menu. When the mouse hovers over them the text changes [color](https://www.mindstick.com/articles/77/how-to-split-form-background-color-in-c-sharp) by:

<Page.Resources>

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

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

<[Style](https://www.mindstick.com/articles/126300/apa-citation-style-get-to-know-about-it-for-your-next-assignment) TargetType="Label" x:Key="menuItemStyle">

<Style.Triggers>

<[Trigger](https://www.mindstick.com/blog/167/triggers-in-wpf) [Property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp)="IsMouseOver" Value="False">

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

</Trigger>

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

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

</Trigger>

</Style.Triggers>

</Style>

</Page.Resources>

<Label x:Name="Label_Video1" Style="{StaticResource menuItemStyle}" [Content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand)="1.Video 1." FontSize="16" HorizontalAlignment="Left" [Margin](https://answers.mindstick.com/qa/104990/what-is-a-margin-call)="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](https://answers.mindstick.com/qa/34556/if-a-user-clicks-all-the-google-ads-on-my-site-is-it-foul) a label I want it to stay a certin color (In this case Gold) and all the [others](https://answers.mindstick.com/qa/33516/what-is-dependency-injection-do-you-use-any-di-library-in-your-project-can-you-name-a-few-of-them-and-why-one-is-better-than-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

## Replies

### Reply by Pravesh Singh

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">    <Grid.Resources>        <Style TargetType="{x:Type RadioButton}">            <Setter Property="Template">                <Setter.Value>                    <ControlTemplate TargetType="{x:Type RadioButton}">                        <TextBlock Text="{TemplateBinding Content}">                            <TextBlock.Style>                                <Style TargetType="{x:Type TextBlock}">                                    <Setter Property="Foreground" Value="White" />                                    <Style.Triggers><DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType={x:Type RadioButton}}}" Value="True">    <Setter Property="Foreground" Value="Gold" /></DataTrigger><DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSourceAncestorType={x:Type RadioButton}}}" Value="True">    <Setter Property="Foreground" Value="Gold" /></DataTrigger>                                    </Style.Triggers>                                </Style>                            </TextBlock.Style>                        </TextBlock>                    </ControlTemplate>                </Setter.Value>            </Setter>        </Style>    </Grid.Resources>    <StackPanel Margin="5" Background="{x:Null}">        <RadioButton Content="1.Video 1." />        <RadioButton Content="2.Video 2." />        <RadioButton Content="3.Video 3." />    </StackPanel></Grid>
```

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.


---

Original Source: https://www.mindstick.com/forum/1772/change-onclick-label-color-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
