ButtonStyle.xaml
View Code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="whiteSolidBrush" Color="White"/> <SolidColorBrush x:Key="blackSolidBrush" Color="Black"/> <LinearGradientBrush x:Key="blackToWhiteLGBrush"> <GradientStop Offset="0" Color="Black"/> <GradientStop Offset="1" Color="White"/> </LinearGradientBrush> <RadialGradientBrush x:Key="blackToWhiteRGBrush"> <GradientStop Offset="0" Color="Black"/> <GradientStop Offset="1" Color="White"/> </RadialGradientBrush> <ControlTemplate x:Key="buttonTemplate" TargetType="Button"> <Grid> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState Name="Normal"/> <VisualState Name="MouseOver"/> <VisualState Name="Pressed"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="Background"> <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource whiteSolidBrush}"/> </ObjectAnimationUsingKeyFrames> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentControl" Storyboard.TargetProperty="Foreground"> <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource blackSolidBrush}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState Name="Disabled"> <Storyboard> <DoubleAnimation Storyboard.TargetName="disabRectangle" Storyboard.TargetProperty="Opacity" To="0.6" Duration="0:0:0"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border Name="border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5"> <ContentControl Name="contentControl" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"/> </Border> <Rectangle Name="disabRectangle" Fill="Gainsboro" Opacity="0"/> </Grid> </ControlTemplate> <Style x:Key="buttonStyle" TargetType="Button"> <Setter Property="BorderBrush" Value="White"/> <Setter Property="BorderThickness" Value="5"/> <Setter Property="BorderBrush" Value="Black"/> <Setter Property="Template" Value="{StaticResource buttonTemplate}"/> </Style> </ResourceDictionary>
使用
<Button x:Name="bt" Content="123" Style="{StaticResource buttonStyle}" IsEnabled="False"/>