如何从代码隐藏中实时更改按钮的整体样式?

发布于 2024-12-06 14:56:49 字数 2022 浏览 1 评论 0原文

我有一个闪烁的按钮。

我想在资源字典闪烁时更改整个按钮样式。

我想它会是这样的:

DesktopWindow.AlertButton.Style = (Style)DesktopWindow.Resources["GreenAlertButtonStyle"];

但这行不通。我该怎么做?我不能简单地更改背景颜色(尽管这就是我真正想做的),因为我想保留触发器。当我现在更改按钮的背景时,鼠标悬停触发器就会停止工作......

按钮:

    <Style TargetType="Button" x:Key="BaseAlertButtonStyle">
    <Setter Property="ToolTip" Value="Show Alert List"/>
    <Setter Property="Effect" Value="{DynamicResource dropShadow}" />
    <Setter Property="Background" Value="{DynamicResource AlertButtonBackground}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch">
                    <Border CornerRadius="5" x:Name="ButtonBorder" Margin="0,0,0,0"
                            VerticalAlignment="Stretch" BorderThickness="0"
                            BorderBrush="#ffffff" Padding="0"
                            Background="{TemplateBinding Background}"
                            HorizontalAlignment="Stretch">
                        <Image x:Name="alertImage">
                            <Image.Source>
                                <BitmapImage UriSource="/resources/alertIcon.png" />
                            </Image.Source>
                        </Image>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="{DynamicResource ButtonRolloverBackground}"/>
                    </Trigger>
                </ControlTemplate.Triggers>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我不想听到有关搜索此问题的消息......

I have a button that flashes.

I would like to change the entire button style from a resource dictionary when ever it flashes.

I would think it would be like this:

DesktopWindow.AlertButton.Style = (Style)DesktopWindow.Resources["GreenAlertButtonStyle"];

But that doesn't work. How do I do this? I cannot simply change the background color (although that's all I really want to do) because I want to preserve the triggers. When ever I change the background of the button right now, the mouseover triggers stop working....

The button:

    <Style TargetType="Button" x:Key="BaseAlertButtonStyle">
    <Setter Property="ToolTip" Value="Show Alert List"/>
    <Setter Property="Effect" Value="{DynamicResource dropShadow}" />
    <Setter Property="Background" Value="{DynamicResource AlertButtonBackground}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch">
                    <Border CornerRadius="5" x:Name="ButtonBorder" Margin="0,0,0,0"
                            VerticalAlignment="Stretch" BorderThickness="0"
                            BorderBrush="#ffffff" Padding="0"
                            Background="{TemplateBinding Background}"
                            HorizontalAlignment="Stretch">
                        <Image x:Name="alertImage">
                            <Image.Source>
                                <BitmapImage UriSource="/resources/alertIcon.png" />
                            </Image.Source>
                        </Image>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="{DynamicResource ButtonRolloverBackground}"/>
                    </Trigger>
                </ControlTemplate.Triggers>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I dont wanna hear it about doing a search for this issue....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

旧夏天 2024-12-13 14:56:49

尝试:

DesktopWindow.AlertButton.Style = FindResource("GreenAlertButtonStyle") as Style;

Try:

DesktopWindow.AlertButton.Style = FindResource("GreenAlertButtonStyle") as Style;
白芷 2024-12-13 14:56:49

明确设置背景后,您必须清除BackgroundProperty,然后设置新样式。

button1.ClearValue(BackgroundProperty);

After setting background explicitly you have to clear BackgroundProperty and then set new style.

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