CHANGE ONCLICK LABEL COLOR WPF

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
WPF 
WPF
 5208  View(s)
Ratings:
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:

<Page.Resources>

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

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

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

        <Style.Triggers>

            <Trigger Property="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="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. Pravesh Singh

    Post:412

    Points:2888
    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">

        <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={RelativeSource

    AncestorType={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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!