MouseOver 触发器在代码隐藏中不起作用?

发布于 2025-01-11 16:36:41 字数 665 浏览 1 评论 0原文

我试图在后面的代码中创建一个 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 buttons 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2025-01-18 16:36:41

假设按钮的 x:Name 等于“Button1”,

您需要在末尾添加:

<Button x:Name="Button1"/>

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);

Button1.Style = style;

Suppose the x:Name of the button is equal to "Button1"

You need to add at the end:

<Button x:Name="Button1"/>

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);

Button1.Style = style;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文