So I have a button which has a style applied to it. The code for the button is this:
<Button Command="w:Command.ShowTBDisplay" Style="{DynamicResource button_displayOptions}" Content="Display TextBlock" Height="34.5" Margin="0,4,0,0" Name="btn_displayTB" />
When the button is clicked, I want to change its background color (permanently, until a different button is clicked). The color that I want to use is a dynamicResource..
Inside the ShowTBDisplay_Executed method, I have code like this:
btn_displayTB.Background = FindResource("cClickColor") as Brush;
But when I do this, nothing happens, no color is set.
I figure that the Style’s color overwrites what ever I try to change it to…but I am not sure.
So how do I go about changing the backColor of a button with a style applied to it.
Here is the style of the button, as well as the color that I wish to use (both styles are in the app.xaml file):
<Style x:Key="button_displayOptions" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Rectangle x:Name="rectangle" Fill="{DynamicResource cMenuBackground}"/>
<ContentPresenter x:Name="tb" TextBlock.Foreground="White" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="8,5,0,5" Height="19.5"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="rectangle" Value="{DynamicResource cMenuItemHighlight}"/>
<Setter Property="TextBlock.Foreground" TargetName="tb" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="cMenuItemHighlight">#FFAFAFAF</SolidColorBrush>
Thanks!
Your template overrides the default one and the
Backgroundproperty of the Button is no longer doing anything, you need to bind some control inside the template to the property using aTemplateBindingfor example.Edit: The rectangle should not have a concrete fill value in the template, this is probably what you want:
Note the new
Setterwhich sets theBackgroundto your resource and the internalTemplateBindingon theRectangle.Fill.