自定义样式例子
样式1,普通按钮
代码
App.xaml
<SolidColorBrush x:Key="SettingButtonBackground" Color="#3f414a" />
<SolidColorBrush x:Key="SettingButtonHover" Color="#5455aa" />
<SolidColorBrush x:Key="SettingButtonPressed" Color="#4385f3" />
<SolidColorBrush x:Key="SettingButtonForgroundColor" Color="White" />
<Style x:Key="SettingButton" TargetType="Button">
<Setter Property="Height" Value="25" />
<Setter Property="Width" Value="80" />
<Setter Property="Background" Value="{StaticResource SettingButtonBackground}" />
<Setter Property="Foreground" Value="{StaticResource SettingButtonForgroundColor}" />
<Setter Property="BorderThickness" Value="1.2" />
<Setter Property="BorderBrush" Value="{StaticResource SettingButtonHover}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource SettingButtonHover}" />
<Setter Property="BorderBrush" Value="{StaticResource SettingButtonHover}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{StaticResource SettingButtonPressed}" />
<Setter Property="BorderBrush" Value="{StaticResource SettingButtonPressed}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.5" />
</Trigger>
</Style.Triggers>
</Style>
ButtonStyleSampleWindow.xaml
<Button
Name="btnStyleSample"
Click="BtnStyleSample_OnClick"
Content="Style Sample"
Style="{StaticResource SettingButton}" />
样式2,带Icon的按钮
代码
App.xaml
<SolidColorBrush x:Key="SettingButtonBackground" Color="#3f414a" />
<SolidColorBrush x:Key="SettingButtonHover" Color="#5455aa" />
<SolidColorBrush x:Key="SettingButtonPressed" Color="#4385f3" />
<SolidColorBrush x:Key="SettingButtonForgroundColor" Color="White" />
<Style x:Key="LittleImageButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource SettingButtonBackground}" />
<Setter Property="Foreground" Value="{StaticResource SettingButtonForgroundColor}" />
<Setter Property="FontSize" Value="12" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image
Name="btnImage"
Grid.Column="0"
Width="16"
Height="16"
Margin="5,5,0,5"
VerticalAlignment="Center"
Source="{TemplateBinding Tag}" />
<ContentPresenter
Grid.Column="1"
Margin="5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource SettingButtonHover}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{StaticResource SettingButtonPressed}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.25" />
</Trigger>
</Style.Triggers>
</Style>
ButtonStyleSampleWindow.xaml
<Button
Name="btnStyleSample2"
Grid.Row="2"
Width="110"
Height="26"
Click="BtnStyleSample_OnClick"
Content="Style Sample"
Style="{StaticResource LittleImageButton}">
<Button.Tag>
<ImageSource>/Images/fire.png</ImageSource>
</Button.Tag>
</Button>
效果