我有一个按钮,我修改了它的模板,所以当按钮被点击和鼠标悬停时它显示不同的背景颜色(它变成绿色而不是浅蓝色)。
这本身就很好用,但我需要根据某些状态更改背景颜色,所以我需要更改代码中的背景。
我的问题是当我更改代码中的背景颜色时,它会正确更改,但不再更改单击或鼠标悬停事件的背景。
我假设我以某种方式覆盖了代码中的背景颜色,因此涵盖了我在模板中创建的内容。如何解决此问题,以便我可以通过代码更改背景但仍然可以单击和鼠标悬停?
这是我的 XAML 代码:
<Style x:Key="NodeButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true" OverridesDefaultStyle="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Microsoft_Windows_Themes:ButtonChrome>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFCFF9C4" Offset="0"/>
<GradientStop Color="#FF249505" Offset="1"/>
<GradientStop Color="#FF58D835" Offset="0.5"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0" Opacity="0.5">
<GradientStop Color="#FFCFF9C4" Offset="0"/>
<GradientStop Color="#FF249505" Offset="1"/>
<GradientStop Color="#FF58D835" Offset="0.5"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在代码中,我只是做 button1.Background = newBrush;
我试图寻找类似的问题,但我找不到任何..任何帮助将不胜感激!我正在使用混合 4 .net 3.5 和 C#。