简体   繁体   English

更改默认悬停在文本颜色WPF上

[英]Change default Hover over text color wpf

I have used to following to change my hover over text color in my labels. 我习惯于按照以下步骤将鼠标悬停在标签上的文本颜色上。 But the default text color is black. 但是默认的文本颜色是黑色。 How do I modify the below have the default color to be something else eg white. 我该如何修改下面的默认颜色,使其成为其他颜色,例如白色。

    <Page.Resources>
    <SolidColorBrush x:Key="mouseOverColor" Color="Gold" />
    <Style x:Key="mouseOverStyle" 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>
</Page.Resources>

You don't need to override template at all. 您根本不需要覆盖模板。 This can be achieved with following style only: 这只能通过以下样式来实现:

<SolidColorBrush x:Key="mouseOverColor" Color="Gold" />
<Style TargetType="Label" x:Key="mouseOverStyle">
   <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Foreground" Value="{StaticResource mouseOverColor}"/>
       </Trigger>
   </Style.Triggers>
</Style>

Your trigger won't work in case you are specifying default value of Foreground on your label. 如果您在标签上指定了Foreground默认值,则触发器将不起作用。

Won't work for this label: 不适用于此标签:

<Label Content="sjdfjfddjfjdfs"
       Style="{StaticResource mouseOverStyle}" Foreground="Green"/>

However, will work for this label: 但是,将适用于此标签:

<Label Content="sjdfjfddjfjdfs" Style="{StaticResource mouseOverStyle}"/>

Even your style will work but make sure you are not setting local value for Foreground DP. 即使您的样式也可以使用,但请确保未设置Foreground DP的本地值。 This is because of Dependency Property Precedence order where local value holds higher precedence than setter values of style. 这是因为Dependency Property Precedence顺序 ,其中本地值的优先级高于样式的setter值。

Have you tried changing the Value for the Foreground property in your Setter ? 您是否尝试过在Setter更改Foreground属性的Value

eg 例如

<Setter Property="Foreground" Value="White" />

Or defining a Brush of your desired color and using that? 或定义所需颜色的Brush并使用它? eg 例如

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

<!-- Your Existing Code -->
    <Setter Property="Foreground" Value="{StaticResource myColor}" />
<!-- Your Existing Code -->

Rohit's answer is much simpler/more sensible if you're just after the label color change (rather than doing anything with the ControlTemplate . 如果您只是在更改标签颜色之后(而不是使用ControlTemplate进行任何操作),则Rohit的答案会更简单/更明智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM