WPF 禁用按钮的背景 [英] WPF Disabled button's background
问题描述
我尝试在禁用按钮时更改按钮的样式:
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>
和我的按钮:
<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?
我认为不是,因为 Windows 中的控件具有默认的 Styles
和 ControlTemplates
,并且 在每个版本的 Windows 中它们都是不同的.另外,样式——它只是很多设置,通常样式不会改变/添加行为到控件,它负责这个ControlTemplate
.
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
.
注:
Style 是一组setter,例如:设置背景、大小等. 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.
在这种情况下,我建议大家更改 ControlTemplate,无论系统版本如何,都可能具有相同的行为(即视图).
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.
在这种情况下,试试这个Style
:
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>
这篇关于WPF 禁用按钮的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!