IsEnabled = False 时的 WPF 按钮背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28877550/
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 Button Background when IsEnabled = False
提问by Fleve
I try to set a style for my button when it is not enabled so that the Background LinearGradientBrush is similar to the one set for the button when it is enabled. I have tried with Triggers but it seems that only the text Color is changing:
当我的按钮未启用时,我尝试为其设置样式,以便背景 LinearGradientBrush 与启用时为该按钮设置的样式相似。我尝试过触发器,但似乎只有文本颜色在改变:
<Application x:Class="RSPolar.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!--Windows style-->
<Style TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.92" />
<GradientStop Color="Gray" Offset="0.9" />
<GradientStop Color="LightGray" Offset="0.88" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<!--Buttons style-->
<Style TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="48" Foreground="#00FF00" Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType={x:Type Button}}}">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="1" Color="DarkGray" ShadowDepth="1" Direction="120"/>
</TextBlock.Effect>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Button.Effect">
<Setter.Value>
<DropShadowEffect Color="Black" Direction="320" ShadowDepth="5" BlurRadius="10" Opacity="0.5" />
</Setter.Value>
</Setter>
<Setter Property="Button.Effect">
<Setter.Value>
<BlurEffect Radius="1"></BlurEffect>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="48" Foreground="#FF0000" Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType={x:Type Button}}}">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="1" Color="DarkGray" ShadowDepth="1" Direction="120"/>
</TextBlock.Effect>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
</Application>
回答by Sheridan
You should not be defining the ContentTemplateproperty. If you want to change what the button looks like, you should define the Templateproperty and add your Triggers into the ControlTemplateyou define there instead.
您不应该定义该ContentTemplate属性。如果您想更改按钮的外观,您应该定义该Template属性并将您的Triggers添加到ControlTemplate您在那里定义的s 中。
Perhaps it would help if you read the Customizing the Appearance of an Existing Control by Creating a ControlTemplatepage on MSDN? You can also find an example of using ControlTemplate.Triggersin the ControlTemplateClasspage on MSDN.
如果您阅读MSDN 上的通过创建 ControlTemplate页面自定义现有控件的外观可能会有所帮助?您还可以在 MSDNControlTemplate.Triggers的ControlTemplateClass页面中找到使用示例。
However, here is a simple example:
但是,这里有一个简单的例子:
In Resources:
在Resources:
<ControlTemplate x:Key="ExampleButton" TargetType="{x:Type Button}">
<Border Background="LightGreen" BorderBrush="Black" BorderThickness="1"
CornerRadius="5" x:Name="Border" Padding="10">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="LightBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
In XAML page:
在 XAML 页面中:
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Template="{StaticResource ExampleButton}" Content="Click me" />
<Button Template="{StaticResource ExampleButton}" Content="Click me"
IsEnabled="False" />
</StackPanel>
UPDATE >>>
更新 >>>
You just need to substitute the 'plain' Backgroundvalue for your LinearGradientBrushas you have in yourexample:
你只需要以替代“普通”Background值你LinearGradientBrush,你中有你的例子:
In Resources:
在Resources:
<ControlTemplate x:Key="ExampleButton" TargetType="{x:Type Button}">
<Border BorderBrush="Black" BorderThickness="1"
CornerRadius="5" x:Name="Border" Padding="10">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Border.Background>
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Why is Target Border setting the color of the Background of the button? Shouldn't it change the color of the border?
为什么目标边框设置按钮背景的颜色?它不应该改变边框的颜色吗?
The Backgroundof the Buttonisthe Backgroundof the Border.
的Background的Button是在Background的Border。

