我尝试在未启用时为我的按钮设置样式,以便后台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>
答案 0 :(得分:6)
您不应该定义ContentTemplate
属性。如果您想更改按钮的外观,则应定义Template
属性,并将Trigger
添加到您在此处定义的ControlTemplate
中。
如果您阅读MSDN上的Customizing the Appearance of an Existing Control by Creating a ControlTemplate页面,也许会有所帮助?您还可以在MSDN上的ControlTemplate
Class页面中找到使用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>
更新&gt;&gt;&gt;
您只需要替换 示例中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>
为什么Target Border设置按钮背景的颜色?它不应该改变边界的颜色吗?
Background
的Button
Background
的{{1}}。
答案 1 :(得分:-1)
您正在明确设置按钮的背景,然后尝试使用触发器进行更改。当IsEnabled属性为true时,您需要移动您的背景为单独的触发器。
<Trigger Property="IsEnabled" Value="False">
...
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<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>