自定义样式例子

样式1,普通按钮

代码

App.xaml

  1. <SolidColorBrush x:Key="SettingButtonBackground" Color="#3f414a" />
  2. <SolidColorBrush x:Key="SettingButtonHover" Color="#5455aa" />
  3. <SolidColorBrush x:Key="SettingButtonPressed" Color="#4385f3" />
  4. <SolidColorBrush x:Key="SettingButtonForgroundColor" Color="White" />
  5. <Style x:Key="SettingButton" TargetType="Button">
  6. <Setter Property="Height" Value="25" />
  7. <Setter Property="Width" Value="80" />
  8. <Setter Property="Background" Value="{StaticResource SettingButtonBackground}" />
  9. <Setter Property="Foreground" Value="{StaticResource SettingButtonForgroundColor}" />
  10. <Setter Property="BorderThickness" Value="1.2" />
  11. <Setter Property="BorderBrush" Value="{StaticResource SettingButtonHover}" />
  12. <Setter Property="Template">
  13. <Setter.Value>
  14. <ControlTemplate TargetType="Button">
  15. <Border
  16. Background="{TemplateBinding Background}"
  17. BorderBrush="{TemplateBinding BorderBrush}"
  18. BorderThickness="{TemplateBinding BorderThickness}">
  19. <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
  20. </Border>
  21. </ControlTemplate>
  22. </Setter.Value>
  23. </Setter>
  24. <Style.Triggers>
  25. <Trigger Property="IsMouseOver" Value="True">
  26. <Setter Property="Background" Value="{StaticResource SettingButtonHover}" />
  27. <Setter Property="BorderBrush" Value="{StaticResource SettingButtonHover}" />
  28. </Trigger>
  29. <Trigger Property="IsPressed" Value="True">
  30. <Setter Property="Background" Value="{StaticResource SettingButtonPressed}" />
  31. <Setter Property="BorderBrush" Value="{StaticResource SettingButtonPressed}" />
  32. </Trigger>
  33. <Trigger Property="IsEnabled" Value="False">
  34. <Setter Property="Opacity" Value="0.5" />
  35. </Trigger>
  36. </Style.Triggers>
  37. </Style>

ButtonStyleSampleWindow.xaml

  1. <Button
  2. Name="btnStyleSample"
  3. Click="BtnStyleSample_OnClick"
  4. Content="Style Sample"
  5. Style="{StaticResource SettingButton}" />

样式2,带Icon的按钮

代码

App.xaml

  1. <SolidColorBrush x:Key="SettingButtonBackground" Color="#3f414a" />
  2. <SolidColorBrush x:Key="SettingButtonHover" Color="#5455aa" />
  3. <SolidColorBrush x:Key="SettingButtonPressed" Color="#4385f3" />
  4. <SolidColorBrush x:Key="SettingButtonForgroundColor" Color="White" />
  5. <Style x:Key="LittleImageButton" TargetType="Button">
  6. <Setter Property="Background" Value="{StaticResource SettingButtonBackground}" />
  7. <Setter Property="Foreground" Value="{StaticResource SettingButtonForgroundColor}" />
  8. <Setter Property="FontSize" Value="12" />
  9. <Setter Property="HorizontalContentAlignment" Value="Center" />
  10. <Setter Property="VerticalContentAlignment" Value="Center" />
  11. <Setter Property="Template">
  12. <Setter.Value>
  13. <ControlTemplate TargetType="Button">
  14. <Grid Background="{TemplateBinding Background}">
  15. <Grid.ColumnDefinitions>
  16. <ColumnDefinition Width="Auto" />
  17. <ColumnDefinition Width="Auto" />
  18. </Grid.ColumnDefinitions>
  19. <Image
  20. Name="btnImage"
  21. Grid.Column="0"
  22. Width="16"
  23. Height="16"
  24. Margin="5,5,0,5"
  25. VerticalAlignment="Center"
  26. Source="{TemplateBinding Tag}" />
  27. <ContentPresenter
  28. Grid.Column="1"
  29. Margin="5"
  30. HorizontalAlignment="Center"
  31. VerticalAlignment="Center"
  32. Content="{TemplateBinding Content}" />
  33. </Grid>
  34. </ControlTemplate>
  35. </Setter.Value>
  36. </Setter>
  37. <Style.Triggers>
  38. <Trigger Property="IsMouseOver" Value="True">
  39. <Setter Property="Background" Value="{StaticResource SettingButtonHover}" />
  40. </Trigger>
  41. <Trigger Property="IsPressed" Value="True">
  42. <Setter Property="Background" Value="{StaticResource SettingButtonPressed}" />
  43. </Trigger>
  44. <Trigger Property="IsEnabled" Value="False">
  45. <Setter Property="Opacity" Value="0.25" />
  46. </Trigger>
  47. </Style.Triggers>
  48. </Style>

ButtonStyleSampleWindow.xaml

  1. <Button
  2. Name="btnStyleSample2"
  3. Grid.Row="2"
  4. Width="110"
  5. Height="26"
  6. Click="BtnStyleSample_OnClick"
  7. Content="Style Sample"
  8. Style="{StaticResource LittleImageButton}">
  9. <Button.Tag>
  10. <ImageSource>/Images/fire.png</ImageSource>
  11. </Button.Tag>
  12. </Button>

效果

button sample.gif