在代码隐藏中触发更新边框但不更新按钮
我有一个在 C# 代码中动态更新的 style
。它适用于一些按钮
和一个边框
。这样我就可以通过简单地设置样式背景属性来更改每个项目的主题。
这很好。我最近想在更改背景时也更改鼠标悬停颜色,因此我实现了以下代码:
Style style = new Style();
// Set background to red
style.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#FF0000") as Brush));
Trigger mouseOver = new Trigger() {
Property = IsMouseOverProperty,
Value = true
};
// Set hover color to green
mouseOver.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#00FF00") as Brush));
style.Triggers.Add(mouseOver);
// Apply style to elements
Application.Current.Resources["menu-color"] = style;
每个项目的背景都已成功设置,但只有边框接受鼠标悬停属性。看看下面的动图。
我不一定希望边框具有悬停颜色,但这是一个问题又过了一天。为什么它只适用于该元素而不适用于其他元素?
I have a style
that is updated dynamically in my C# code. It applies to a few button
s and a border
. This way I can change the theme of every item by simply setting the style background property.
This is fine. I recently wanted to change the mouse-over color as well when I change the background so I implemented the following code:
Style style = new Style();
// Set background to red
style.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#FF0000") as Brush));
Trigger mouseOver = new Trigger() {
Property = IsMouseOverProperty,
Value = true
};
// Set hover color to green
mouseOver.Setters.Add(new Setter(Button.BackgroundProperty, new BrushConverter().ConvertFrom("#00FF00") as Brush));
style.Triggers.Add(mouseOver);
// Apply style to elements
Application.Current.Resources["menu-color"] = style;
The background is set successfully on every item but only the border accepts the mouse-over property. Take a look at the gif below.
I don't necessarily want the border to have a hover color, but that's a problem for another day. Why does it only apply to that element but none of the others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Xaml 具有内置触发器。由于 Button 有默认的
IsMouseOver
样式,因此您必须使用样式来自定义它。此示例显示了按钮上自定义样式的用法。
样式可以放置在您的
中,它位于App.xaml
中,这还有一个好处是不必创建自定义控件来模仿本机按钮控制!
此代码来自我的 GitHub 如果您想查看更多例子!
Xaml has triggers built-in. Because a Button has a default
IsMouseOver
style, you have to use styling to customize it.This example shows the usage of a custom style on a button.
Styling can be placed in your
<Application.Resources>
Which is located inApp.xaml
This also comes with the benefit of not having to create custom controls to mimic the native Button controls!
This code is from my GitHub if you want to look at more examples!