如何删除 WPF 按钮上的默认鼠标悬停效果?
我的问题是,在 WPF 中,每当我尝试使用触发器或动画更改按钮背景的颜色时,默认的鼠标悬停效果(带有橙色光芒的灰色)似乎优先。
经过大量搜索后,我对如何消除这种影响一无所知。
My problem is that in WPF, whenever I try and change the colour of a button's background using triggers or animations, the default mouseover effect (of being grey with that orange glow) seems to take priority.
After extensive searches I'm clueless as to how to remove this effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这与 Mark Heath 提到的解决方案类似,但没有那么多代码来创建一个非常基本的按钮,没有内置的鼠标悬停动画效果。它保留了简单的鼠标悬停效果,以黑色显示按钮边框。
例如,可以将样式插入到 Window.Resources 或 UserControl.Resources 部分(如图所示)。
This is similar to the solution referred by Mark Heath but with not as much code to just create a very basic button, without the built-in mouse over animation effect. It preserves a simple mouse over effect of showing the button border in black.
The style can be inserted into the Window.Resources or UserControl.Resources section for example (as shown).
只需添加一个非常简单的解决方案,这对我来说就足够了,我认为解决了OP的问题。我在这个答案中使用了解决方案,除了使用常规的
背景
值而不是图像。除了强制
Background
始终为模板化按钮的Transparent
背景之外,无需重新模板 - 完成此操作后,鼠标悬停将不再影响背景。显然将Transparent
替换为任何首选值。Just to add a very simple solution, that was good enough for me, and I think addresses the OP's issue. I used the solution in this answer except with a regular
Background
value instead of an image.No re-templating beyond forcing the
Background
to always be theTransparent
background from the templated button - mouseover no longer affects the background once this is done. Obviously replaceTransparent
with any preferred value.您需要创建自己的自定义按钮模板才能完全控制所有状态下的外观。这是教程 。
You need to create your own custom button template to have full control over the appearance in all states. Here's a tutorial.
松饼人有一个非常简单的答案,对我有用。
要添加更具体的方向,至少对于 VS 2013:
... Style="{ DynamicResource MouseOverNonDefault}"/>
The Muffin Man had a very simple answer which worked for me.
To add a little more specific direction, at least for VS 2013:
... Style="{DynamicResource MouseOverNonDefault}"/>
这个链接对我帮助很大
http://www.codescratcher.com/ wpf/remove-default-mouse-over-effect-on-wpf-buttons/
在 UserControl.Resources 或 Window.Resources 中定义样式
,然后添加以这种方式为您的按钮设置样式 Style="{StaticResource MyButton}"
This Link helped me alot
http://www.codescratcher.com/wpf/remove-default-mouse-over-effect-on-wpf-buttons/
Define a style in UserControl.Resources or Window.Resources
Then add the style to your button this way Style="{StaticResource MyButton}"
如果有人不想覆盖默认的控制模板,那么这里是解决方案。
您可以为可以具有 TextBlock 的按钮创建 DataTemplate,然后您可以在 IsMouseOver 属性上编写属性触发器以禁用鼠标悬停效果。 TextBlock 和 Button 的高度应该相同。
If someone doesn't want to override default Control Template then here is the solution.
You can create DataTemplate for button which can have TextBlock and then you can write Property trigger on IsMouseOver property to disable mouse over effect. Height of TextBlock and Button should be same.
dodgy_coder 答案的扩展,添加了对..
添加对 IsSelected 和悬停(即切换按钮)的支持
示例。
An extension on dodgy_coder's answer which adds support for..
Adds support for IsSelected and hover, i.e. a toggled button
examples..
<Button Content="Wipe On" Selector.IsSelected="True" />
<Button Content="Wipe Off" Selector.IsSelected="False" />
使用模板触发器:
Using a template trigger: