在代码隐藏中触发更新边框但不更新按钮

发布于 2025-01-11 23:02:24 字数 967 浏览 0 评论 0原文

我有一个在 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 buttons and a border. This way I can change the theme of every item by simply setting the style background property.

Working example

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.

Hover colors

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 技术交流群。

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

发布评论

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

评论(1

白日梦 2025-01-18 23:02:24

Xaml 具有内置触发器。由于 Button 有默认的 IsMouseOver 样式,因此您必须使用样式来自定义它。

此示例显示了按钮上自定义样式的用法。
样式可以放置在您的 中,它位于 App.xaml 中,

这还有一个好处是不必创建自定义控件来模仿本机按钮控制!

<!-- Apply styling in xaml like so -->
<!-- Note that the x:Key here is what sets the style -->
<Button Style="{StaticResource ButtonStyle}" />

<!-- Add to App.xaml -->
<!-- If an x:key is not set, the styling will apply to all elements that have the given TargetType -->
<Style TargetType="Button" x:Key="ButtonStyle">
            <Setter Property="Background" Value="Cyan"/>
            <Setter Property="Height" Value="60" />

            <!-- Template can be used to take over styling of an element -->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}">
                            <!-- Here goes your custom style if you want one-->
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <!-- This will be 'called' whenever the mouse is over the button -->
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
            </Style.Triggers>
        </Style>

此代码来自我的 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 in App.xaml

This also comes with the benefit of not having to create custom controls to mimic the native Button controls!

<!-- Apply styling in xaml like so -->
<!-- Note that the x:Key here is what sets the style -->
<Button Style="{StaticResource ButtonStyle}" />

<!-- Add to App.xaml -->
<!-- If an x:key is not set, the styling will apply to all elements that have the given TargetType -->
<Style TargetType="Button" x:Key="ButtonStyle">
            <Setter Property="Background" Value="Cyan"/>
            <Setter Property="Height" Value="60" />

            <!-- Template can be used to take over styling of an element -->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}">
                            <!-- Here goes your custom style if you want one-->
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <!-- This will be 'called' whenever the mouse is over the button -->
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
            </Style.Triggers>
        </Style>

This code is from my GitHub if you want to look at more examples!

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