我试图在按钮未启用时为其设置样式,以便Background 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>
您不应该定义ContentTemplate
属性。如果您想更改按钮的外观,您应该定义Template
属性,并将您的Trigger
添加到您在那里定义的ControlTemplate
中。
如果您阅读MSDN上的"通过创建控件模板自定义现有控件的外观"页面,也许会有所帮助?您还可以在MSDN上的ControlTemplate
类页面中找到使用ControlTemplate.Triggers
的示例。
然而,这里有一个简单的例子:
在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>
在XAML页面中:
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Template="{StaticResource ExampleButton}" Content="Click me" />
<Button Template="{StaticResource ExampleButton}" Content="Click me"
IsEnabled="False" />
</StackPanel>
更新>>>
您只需要将"普通"Background
值替换为LinearGradientBrush
,如您的示例中所示:
在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>
为什么目标边框设置按钮的背景颜色?它不应该改变边框的颜色吗?
Button
的Background
是Border
的Background
。