C# 如何实时地从代码隐藏更改按钮的整个样式?
我有一个闪烁的按钮 我想在资源字典闪烁时更改整个按钮样式 我想是这样的:C# 如何实时地从代码隐藏更改按钮的整个样式?,c#,wpf,button,triggers,background,C#,Wpf,Button,Triggers,Background,我有一个闪烁的按钮 我想在资源字典闪烁时更改整个按钮样式 我想是这样的: DesktopWindow.AlertButton.Style = (Style)DesktopWindow.Resources["GreenAlertButtonStyle"]; 但这不起作用。我该怎么做?我不能简单地改变背景颜色(尽管这是我真正想要做的),因为我想保留触发器。当我现在更改按钮的背景时,鼠标悬停触发器停止工作 按钮: <Style TargetType="Button" x:Key="Ba
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>
我不想听到关于搜索此问题的消息….试试:
DesktopWindow.AlertButton.Style = FindResource("GreenAlertButtonStyle") as Style;
明确设置背景后,必须清除BackgroundProperty,然后设置新样式
button1.ClearValue(BackgroundProperty);
您是否检查了DesktopWindow.Resources[“GreenAlertButtonStyle”]是否不返回空值?是的,所有样式都存在,甚至设置时都没有出现问题。说清楚一点,我想清除旧样式并引入新样式(在显式设置其背景后)不起作用,但谢谢。为了清楚起见,我想清除旧样式,引入新样式。会发生什么情况,
FindResource
会返回任何内容吗?是的,我快速观察了返回的对象,它实际上是正确的样式。交换样式前的片刻。我确实明确地设置了按钮背景。。。我不知道这和它有什么关系。谢谢。你可以改用样式选择器()吗?这很有效。非常感谢你为我指明了正确的方向。