C# 如何使用触发器或任何其他事件更改 wpf 中 onmoveover、onmouseleave 的按钮颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/934442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the button color onmoveover,onmouseleave in wpf by using triggers or any other events
提问by ibrahimkhan
I have developed a WPF Application with some buttons. Now i want to change the color of those buttons onmouseover,onmouseleave,onmouseenter by using triggers or any other events. Any suggestion plz Thanks in advance.
我开发了一个带有一些按钮的 WPF 应用程序。现在我想通过使用触发器或任何其他事件来更改 onmouseover、onmouseleave、onmouseenter 上这些按钮的颜色。任何建议请提前致谢。
回答by Steve Dignan
Inside the desired event, you can set the background color like this...
在所需的事件中,您可以像这样设置背景颜色...
// Change the background color of button1 to Blue
button1.Background = Brushes.Blue;
You can also set this in a trigger:
您还可以在触发器中设置它:
<!-- Button will change from Blue to Yellow on MouseOver -->
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Blue" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Yellow" />
</Trigger>
</Style.Triggers>
</Style>
For even more details, check out the Property Triggerssection of thisarticle.
如需详情,请查看属性触发器的部分这一文章。