【问题标题】:Change OnClick label color WPF更改 OnClick 标签颜色 WPF
【发布时间】:2013-12-15 12:43:09
【问题描述】:

我来自 C# winforms 背景,我通常会在代码中完成所有这些工作。 我有几个标签用作菜单。 当鼠标悬停在它们上面时,文本会改变颜色:

<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"/>

当用户单击标签时,我希望它保持某种颜色(在本例中为金色),而所有其他标签保持正常颜色。因此,如果之前点击过一个标签,然后我点击另一个标签,它将从金色变为白色等

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    在使用 WPF 时,您必须稍有不同的想法。我们知道Label 控件不知道它何时被单击,尤其是它不知道另一个Label 元素何时被单击...但有些控件可以。考虑一下... RadioButton 正是这种行为。现在这就是 WPF 真正大放异彩的地方。

    我们可以使用RadioButtons,并通过为它们提供一个新的ControlTemplate,我们可以让它们看起来像纯文本:

    <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>
    

    如果您不希望 RadioButtons 都在一个 StackPanel 中,那么您可以使用为它们提供相同的 GroupName 属性值,以确保它们仍然作为一个组运行。

    【讨论】:

    • 感谢这招。但是我想让我的文本变成金色,如果它被点击或者鼠标悬停在它上面。有没有办法可以将两个触发器组合在一起??
    • 我知道了 我刚刚添加了以下内容:
    • 抱歉,我没有注意到其他要求。我已经相应地更新了我的答案。请注意,您应该使用我添加的DataTrigger,它将与RadioButton 一起使用,而不仅仅是TextBlock...您会发现它的效果更好。您还可以为 IsPressed 属性添加另一个 DataTrigger 以突出显示用户实际单击其中一个的时间。
    • 感谢您的帮助。还有一件事,我在代码中添加了将光标更改为一只手的代码,但是当我单击鼠标时,手会变回箭头。我只是在您设置前景色的地方添加了 。它确实会在鼠标悬停时变成一只手,但是当我单击它时它会变回来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2011-09-08
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多