当“IsMouseOver"出现时更改 WPF 按钮前景是真的 [英] Change WPF Button Foreground when the "IsMouseOver" is True

查看:32
本文介绍了当“IsMouseOver"出现时更改 WPF 按钮前景是真的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些类似的问题,但似乎没有一个解决方案有效.

I have seen a few similar questions but not of the solutions seem to work.

我更愿意通过 XAML 获得解决方案,但也不介意通过代码 (VB.net).

I would prefer to have the solution through XAML but wouldn't mind it through code (VB.net) either.

我目前有这个代码,不知道有多少是正确的:

I have this code currently, not sure how much of it is correct:

<Style TargetType="{x:Type Button}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Foreground" Value="DarkSlateGray"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="White">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="DodgerBlue"/>
                </Trigger>
            </Style.Triggers>
        </Style>

提前致谢.

推荐答案

大体上是正确的.但请注意,模板中的 ContentPresenter 没有定义 Content 属性.当然,如果你需要更复杂的触发器,你也可以结合IsEnabled等其他东西组合成一个MultiTrigger.

It is generally correct. But do note that your ContentPresenter in your template has no Content property defined. Of course, if you need more complicated triggers, you can also combine with other things like IsEnabled into a MultiTrigger.

这篇关于当“IsMouseOver"出现时更改 WPF 按钮前景是真的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆