WPF - 应用前景时默认按钮禁用样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20796465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - Default Button Disable Style When Foreground applied
提问by Sankarann
Button is applied with a Foregroundwhen it is enabled. If it is Set as Disabled, default Button's Disabled Foregroundneed to apply.
按钮Foreground在启用时应用 a 。如果设置为禁用,则Foreground需要应用默认按钮的禁用。
<Button Width="150"
Height="50"
Content="Test"
Foreground="Red"
IsEnabled="False" />
I have Triggers for this button like
我有这个按钮的触发器,比如
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#FFADADAD"/>
</Trigger>
This Foregroundis not applied when it is enabled.
这Foreground在启用时不适用。
Any idea on this?
对此有什么想法吗?
Need like below,
需要像下面,




回答by Mohd Ahmed
If you set the properties locally the trigger will not be able to change the value due to precedence.
如果您在本地设置属性,由于优先级,触发器将无法更改该值。
Move the Foreground and IsEnabled property into the style:
将 Foreground 和 IsEnabled 属性移动到样式中:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#FFADADAD"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<Button Width="150"
Height="50"
Content="Test">
</Button>
回答by har07
I am not sure I understand the question properly, because those codes posted in question should work fine. Following is a working sample to produce a Button exactly as the captured image shows. I put the Style as resource in the Button's container control -StackPanel in this case- :
我不确定我是否正确理解了这个问题,因为这些有问题的代码应该可以正常工作。以下是一个工作示例,用于生成与捕获的图像显示的完全相同的 Button。在这种情况下,我将样式作为资源放在 Button 的容器控件 -StackPanel 中:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#FFADADAD"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<Button Width="150"
Height="50"
Content="Test"
IsEnabled="True">
</Button>
</StackPanel>
回答by Boopesh
If you edit Button's default template you can find the following trigger inside control template.
如果您编辑 Button 的默认模板,您可以在控件模板中找到以下触发器。
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
<Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
</Trigger>
This trigger set Foreground to the content presenter using TargetName. No template binding used here. It directly assigns value. So the values you set for Foreground in Style Triggers will not be applied. You can edit the control template to achieve your requirement. This limitation not only for Foreground, for Background and BorderBrush also have this.
此触发器使用 TargetName 将 Foreground 设置为内容演示者。这里没有使用模板绑定。它直接赋值。因此,您为 Foreground in Style Triggers 设置的值将不会被应用。您可以编辑控制模板来实现您的要求。这个限制不仅对于Foreground,对于Background 和BorderBrush 也有这个。
回答by user3707094
The following code is working for me:
以下代码对我有用:
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid x:Name="grid" Background="{StaticResource FlatButtonNavigation}">
<Border Name="ButtonBorder" CornerRadius="5,5,5,5"
BorderBrush="Gray" BorderThickness="2">
<ContentPresenter Name="MyContentPresenter"
HorizontalAlignment="center"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
TextBlock.FontSize="{TemplateBinding FontSize}"
TextBlock.Foreground="{StaticResource LeftPanelFontColor}"
Margin="10"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="ButtonBorder" Property="Background" Value="LightGray"/>
<Setter TargetName="ButtonBorder" Property="BorderBrush" Value="#FFADB2B5"/>
<Setter TargetName="MyContentPresenter" Property="TextBlock.Foreground" Value="White"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

