在 Silverlight/WPF 按钮上设置 MouseOver 的样式 [英] Style the MouseOver on a Silverlight/WPF button

查看:38
本文介绍了在 Silverlight/WPF 按钮上设置 MouseOver 的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为鼠标悬停在按钮上的样式而苦苦挣扎......我已经设法为按钮设置了样式(纯红色),但我希望在鼠标悬停时将其更改为纯黑色.我是 XAML 的新手,我可以看到它需要某种类型的故事板/动画......不确定如何做到这一点.

Struggling with styling the mouse over for a button ... I have managed to style the button (solid red), but I would like for it to change to solid black whenever a mouse over occurs. I am new to XAML, and I can see that it wants some type of story board/animation ... not sure exactly how to do this.

任何帮助将不胜感激.

推荐答案

这与 WPF 和 Silverlight 不同.在 WPF 中,Rob 的回答是正确的.

This is different from WPF to Silverlight. In WPF, the answer from Rob is correct.

在 Silverlight 中,这不起作用.Silverlight 使用 VisualStateManager 而不是触发器.这个的代码比较复杂,但也有人觉得这样更好.您最终不得不以您的风格创建一个控件模板.(有关定义控件模板的信息,请参阅 这篇文章.创建类似 ControlTemplate 的最简单方法是使用 Expression Blend,它具有为您完整提取现有模板的功能.)

In Silverlight, this won't work. Silverlight uses the VisualStateManager instead of triggers. The code for this is more complex, but some people feel that this is better. You wind up having to create a control template in your style. (For information on defining the control template, see This Article. The easiest way to create a similar ControlTemplate is to use Expression Blend, which has a function to extract the existing template in full for you.)

在控件模板中,定义您关心的 VisualState 以及您希望发生的事情.

In the control template, define the VisualState you care about and what you want to happen.

<VisualStateGroup x:Name="CommonStateGroup">
    <VisualState x:Name="MouseOverState">
        <Storyboard>
            <ColorAnimation Storyboard.TargetName="TopmostElementOfTheTemplate" 
                                       Storyboard.TargetProperty="Foreground" 
                                       To="Black"
                                       Duration="00:00:00" >
            </ColorAnimation>
        </Storyboard>
    </VisualState>
</VisualStateGroup>
...

在样式中指定默认前景色也很重要,就像 Rob 上面所做的那样.如果您在控件上指定它,它将覆盖样式中的值.

It is important to specify the default foreground color in the style as well, as Rob did above. If you specify it on the control instead it will override values from the style.

请注意,可以从 WPF Toolkit 中获取 VisualStateManager,以便在 WPF 中使用类似的解决方案.

Note that it is possible to get the VisualStateManager out of the WPF Toolkit to have a similar solution in WPF.

这篇关于在 Silverlight/WPF 按钮上设置 MouseOver 的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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