I've got a button that I've modified its template so it shows different background colour when the button gets clicked and mouseover (it goes green rather than light blue).
That works great by itself but I need to change the background colour according to some some state so I need to change the background in the code.
My problem is when I change the background colour in the code, it changes correctly but it's no longer changing background for click or mouseover events.
I'm assuming that I somehow overwritten the background colour in the code so what I created in the template is covered. How can I fix this problem so I can change the background through the code but still has the action to click and mouseover?
Here's my XAML code:
<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>
In the code, I just do button1.Background = newBrush;
I've tried to look for similar questions but I couldn't locate any.. Any help would be appreciated! I'm using blend 4 .net 3.5 and C#.
I believe you're running into a DependecyProperty value precedence issue. Setting the value in code overrides the values set in your style. Instead of setting the property directly in the back code try changing the style on the button. Another solution would be to bind your background to a property on your DataContext and have it set there rather than in your style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With