我正在尝试创建一个按钮模板,以防止控件在禁用时变灰,它的工作很好,但出现问题的原因是当我在按钮属性中设置颜色后,它不会改变背景色。
这是我制作的按钮:
<Style TargetType="Button" x:Key="TestButton">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="MinHeight" Value="29px" />
<Setter Property="MinWidth" Value="103px" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="#EEEEEE"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border TextBlock.Foreground="{TemplateBinding Foreground}" x:Name="Border">
<Border.Background>
<SolidColorBrush Color="{TemplateBinding Background}" />
</Border.Background>
<Border.BorderBrush>
<SolidColorBrush Color="Black" />
</Border.BorderBrush>
<Border.BorderThickness>
<Thickness Top="0.75" Bottom="0.75" Right="0.75" Left="0.75"/>
</Border.BorderThickness>
<Border.CornerRadius>
<CornerRadius TopLeft="3" TopRight="3" BottomLeft="3" BottomRight="3"/>
</Border.CornerRadius>
<ContentPresenter Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True" />
</Border>
<!--Triggers-->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#E1F3FD" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="#C4E9FF" />
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Color="Black" Direction="500" ShadowDepth="1" BlurRadius="5" Opacity="0.5" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Background}"/>
<Setter Property="Opacity" Value="0.5" />
</Trigger>
<Trigger Property="IsEnabled" Value="True" >
<Setter TargetName="Border" Property="Background" Value="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Background}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我调用它时,会给我两个不同的结果,有效的结果如下所示:<Button Style="{DynamicResource ResourceKey=TestButton}" Content="Button" Height="66" Name="button1" Width="149" Click="button1_Click" />
但是当我给它添加一个背景时,由于某种原因它停止了更改背景:
<Button Style="{DynamicResource ResourceKey=TestButton}" Background="Orange" Content="Button" Height="66" Name="button1" Width="149" Click="button1_Click" />
Background="Orange"
添加到样式中,你就可以开始了。 - Metro Smurf