I try to change button's style when it is disabled:
<Style TargetType="Button" x:Key="MyButton2">
<Setter Property="Background" Value="MediumAquamarine" />
<Setter Property="Foreground" Value="MediumBlue" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="DeepPink"/>
</Trigger>
</Style.Triggers>
</Style>
and my button:
<Button Style="{StaticResource MyButton2}" Content="My text" Width="100" Height="30" IsEnabled="False" />
But for some reason This style is not applied to the button:
How can i apply this style to my button? And can i do it without overriding button's template only with styles?
Can I do it without overriding button's template only with styles?
I think not, because the Controls in the Windows has a default Styles
and ControlTemplates
and in each version of Windows they are different. In addition, the styles - it's just a lot of settings, usually the style does not change/add behavior to control, which is responsible for this ControlTemplate
.
Note:
the Style it is a set of setters, for example: set the background, size, etc. The ControlTemplate
is the form, in which all of these settings will appear.
In this situation, I recommend you all to change the ControlTemplate, where it is possible to have the same behavior aka view, regardless of the version of the system.
In this case, try this Style
:
<Window.Resources>
<Style TargetType="Button" x:Key="MyButton2">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Background" Value="MediumAquamarine" />
<Setter Property="Foreground" Value="MediumBlue" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter x:Name="MyContentPresenter"
Content="{TemplateBinding Content}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="DeepPink"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Button Content="Button"
IsEnabled="False"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="75"
Style="{StaticResource MyButton2}"
Click="Button_Click"/>
</Grid>