MouseOver 触发器在代码隐藏中不起作用?
我试图在后面的代码中创建一个 MouseOver
样式。我已经在 XAML 中成功完成了此操作,但我需要在 C# 中完成此操作。这是我的样式代码:
Style style = new Style();
style.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#FF0000") as Brush));
Trigger mouseOver = new Trigger() {
Property = IsMouseOverProperty,
Value = true,
};
mouseOver.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#00FF00") as Brush));
style.Triggers.Add(mouseOver);
按钮
在 XAML 代码中应用样式。当此代码运行时,我可以看到它们改变颜色,但 MouseOver
永远不会改变。我查看了数十篇有关此问题的 MSDN 文章和 Stackoverflow 帖子,但没有一个解决方案适合我的情况。
我在这里做错了什么?
I am trying to create a MouseOver
style in the code behind. I have done this succesfully in XAML, but I need to do it in C#. Here is my style code:
Style style = new Style();
style.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#FF0000") as Brush));
Trigger mouseOver = new Trigger() {
Property = IsMouseOverProperty,
Value = true,
};
mouseOver.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#00FF00") as Brush));
style.Triggers.Add(mouseOver);
The button
s apply the style in the XAML code. I can see them change color when this code is run, but the MouseOver
never changes. I've looked at dozens of MSDN articles and Stackoverflow posts about this but none of the solutions worked in my case.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设按钮的 x:Name 等于“Button1”,
您需要在末尾添加:
Suppose the x:Name of the button is equal to "Button1"
You need to add at the end: