WPF 控件模板 Simple Styles.xaml

这个ResourceDictionary定义了一组控制样式,为创建自定义控件提供简化起点。包含各种颜色刷(如正常背景、边框、高亮等)和控件样式,如SimpleButton、RadioButton、CheckBox和TreeView等。样式考虑了不同状态(如鼠标悬停、按下、选中和禁用)下的外观,并使用动态资源进行绑定。
摘要由CSDN通过智能技术生成

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
 
 <!-- SimpleStyles.XAML defines a set of control styles which are simplified starting points for creating your own controls -->
 
 <!-- Brushes : These are used to define the color for background, foreground, selection, enabled etc of all controls
 If you want to change the color of a control you can just chnage the brush; if you want to add a new shape or change arrangement then also edit the template -->
 
 <!-- NormalBrush is used as the Background for SimpleButton, SimpleRepeatButton -->
 <LinearGradientBrush x:Key="NormalBrush" EndPoint="0,1" StartPoint="0,0">
  <GradientStop Color="#EEE" Offset="0.0"/>
  <GradientStop Color="#CCC" Offset="1.0"/>
 </LinearGradientBrush>
 <LinearGradientBrush x:Key="NormalBorderBrush" EndPoint="0,1" StartPoint="0,0">
  <GradientStop Color="#CCC" Offset="0.0"/>
  <GradientStop Color="#444" Offset="1.0"/>
 </LinearGradientBrush>
 
 <!-- LightBrush is used for content areas such as Menu, Tab Control background -->
 <LinearGradientBrush x:Key="LightBrush" EndPoint="0,1" StartPoint="0,0">
  <GradientStop Color="#FFF" Offset="0.0"/>
  <GradientStop Color="#EEE" Offset="1.0"/>
 </LinearGradientBrush>
 
 <!-- MouseOverBrush is used for MouseOver in Button, Radio Button, CheckBox -->
 <LinearGradientBrush x:Key="MouseOverBrush" EndPoint="0,1" StartPoint="0,0">
  <GradientStop Color="#FFF" Offset="0.0"/>
  <GradientStop Color="#AAA" Offset="1.0"/>
 </LinearGradientBrush>
 
 <!-- PressedBrush is used for Pressed in Button, Radio Button, CheckBox -->
 <LinearGradientBrush x:Key="PressedBrush" EndPoint="0,1" StartPoint="0,0">
  <GradientStop Color="#BBB" Offset="0.0"/>
  <GradientStop Color="#EEE" Offset="0.1"/>
  <GradientStop Color="#EEE" Offset="0.9"/>
  <GradientStop Color="#FFF" Offset="1.0"/>
 </LinearGradientBrush>
 <LinearGradientBrush x:Key="PressedBorderBrush" EndPoint="0,1" StartPoint="0,0">
  <GradientStop Color="#444" Offset="0.0"/>
  <GradientStop Color="#888" Offset="1.0"/>
 </LinearGradientBrush>

 <!-- SelectedBackgroundBrush is used for the Selected item in ListBoxItem, ComboBoxItem-->
 <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD"/> 

 <!-- Disabled Brushes are used for the Disabled look of each control -->
 <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888"/>
 <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE"/>
 <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA"/>

 <!-- Used for background of ScrollViewer, TreeView, ListBox, Expander, TextBox, Tab Control -->
 <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF"/>
 
 <!-- DefaultedBorderBrush is used to show KeyBoardFocus -->
 <LinearGradientBrush x:Key="DefaultedBorderBrush" EndPoint="0,1" StartPoint="0,0">
  <GradientStop Color="#777" Offset="0.0"/>
  <GradientStop Color="#000" Offset="1.0"/>
 </LinearGradientBrush>

 <SolidColorBrush x:Key="SolidBorderBrush" Color="#888"/>
 <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA"/>
 <SolidColorBrush x:Key="LightColorBrush" Color="#DDD"/>
 
 <!-- Used for Checkmark, Radio button, TreeViewItem, Expander ToggleButton glyphs -->
 <SolidColorBrush x:Key="GlyphBrush" Color="#444"/>
 
 
 <!-- Style and Template pairs are used to define each control Part -->
 <!-- The Style provides default values on the control; the Template gives the elements for each control -->
 
 <!-- SimpleButtonFocusVisual is used to show keyboard focus around a SimpleButton control -->
 <Style x:Key="SimpleButtonFocusVisual">
  <Setter Property="Control.Template">
   <Setter.Value>
    <ControlTemplate>
     <Border>
      <Rectangle Margin="2" Stroke="#60000000" StrokeThickness="1" StrokeDashArray="1 2"/>
     </Border>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
 
 <!-- Simple Button - This control sets brushes on each state. Note that these brushes must be listed above since they are static resources -->
 <Style x:Key="SimpleButton" TargetType="{x:Type Button}" BasedOn="{x:Null}">
  <Setter Property="FocusVisualStyle" Value="{DynamicResource SimpleButtonFocusVisual}"/>
  <Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
     
     <!-- We use Grid as a root because it is easy to add more elements to customize the button -->
     <Grid x:Name="Grid">
      <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"/>
      
      <!-- Content Presenter is where the text content etc is placed by the control -->
      <!-- The bindings are useful so that the control can be parameterized without editing the template -->
      <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
     </Grid>
     
     <!--Each state sets a brush on the Border in the template -->
     <ControlTemplate.Triggers>
      <Trigger Property="IsKeyboardFocused" Value="true">
       <Setter Property="BorderBrush" Value="{DynamicResource DefaultedBorderBrush}" TargetName="Border"/>
      </Trigger>
      <Trigger Property="IsMouseOver" Value="true">
       <Setter Property="Background" Value="{DynamicResource MouseOverBrush}" TargetName="Border"/>
      </Trigger>
      <Trigger Property="IsPressed" Value="true">
       <Setter Property="Background" Value="{DynamicResource PressedBrush}" TargetName="Border"/>
       <Setter Property="BorderBrush" Value="{DynamicResource PressedBorderBrush}" TargetName="Border"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="true"/>
      <Trigger Property="IsEnabled" Value="false">
       <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
       <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border"/>
       <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
      </Trigger>
     </ControlTemplate.Triggers>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
 
 <Style x:Key="RadioButtonFocusVisual">
  <Setter Property="Control.Template">
   <Setter.Value>
    <ControlTemplate>
     <Border>
      <Rectangle Margin="15,0,0,0" Stroke="#60000000" StrokeThickness="1" StrokeDashArray="1 2"/>
     </Border>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
 
 <Style x:Key="CheckBoxFocusVisual">
  <Setter Property="Control.Template">
   <Setter.Value>
    <ControlTemplate>
     <Border>
      <Rectangle Margin="15,0,0,0" Stroke="#60000000" StrokeThickness="1" StrokeDashArray="1 2"/>
     </Border>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
 
 <!-- Simple CheckBox -->
 <Style x:Key="SimpleCheckBox" TargetType="{x:Type CheckBox}">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="FocusVisualStyle" Value="{DynamicResource CheckBoxFocusVisual}"/>
  <Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type CheckBox}">
     
     <!-- BulletDecorator is used to provide baseline alignment between the checkmark and the Content -->
     <BulletDecorator Background="Transparent">
      <BulletDecorator.Bullet>
       <Grid Width="13" Height="13">
        <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
        <Path x:Name="CheckMark" Stroke="{DynamicResource GlyphBrush}" StrokeThickness="2" SnapsToDevicePixels="False" Data="M 0 0 L 13 13 M 0 13 L 13 0"/>
       </Grid>
      </BulletDecorator.Bullet>
      <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
     </BulletDecorator>
     
     <!-- This uses Visibility to hide and show the CheckMark on IsChecked -->
     <ControlTemplate.Triggers>
      <Trigger Property="IsChecked" Value="false">
       <Setter Property="Visibility" Value="Collapsed" TargetName="CheckMark"/>
      </Trigger>
      <Trigger Property="IsMouseOver" Value="true">
       <Setter Property="Background" Value="{DynamicResource MouseOverBrush}" TargetName="Border"/>
      </Trigger>
      <Trigger Property="IsPressed" Value="true">
       <Setter Property="Background" Value="{DynamicResource PressedBrush}" TargetName="Border"/>
       <Setter Property="BorderBrush" Value="{DynamicResource PressedBorderBrush}" TargetName="Border"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
       <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
       <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border"/>
      </Trigger>
     </ControlTemplate.Triggers>
     
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
 
 <!-- Simple Radio Button -->
 <Style x:Key="SimpleRadioButton" TargetType="{x:Type RadioButton}">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="FocusVisualStyle" Value="{DynamicResource RadioButtonFocusVisual}"/>
  <Setter Property="Background" Value="{DynamicResource NormalBrush}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/>
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type RadioButton}">
    
     <!-- BulletDecorator is used to provide baseline alignment between the checkmark and the Content -->
     <BulletDecorator Background="Transparent">
      <BulletDecorator.Bullet>
       <Grid Width="13" Height="13">
        <Ellipse x:Name="Ellipse_Border" Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="1"/>
        <Ellipse Margin="4" x:Name="CheckMark" Fill="{DynamicResource GlyphBrush}"/>
       </Grid>
      </BulletDecorator.Bullet>
      <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
     </BulletDecorator>
     
     <ControlTemplate.Triggers>
      <Trigger Property="IsChecked" Value="false">
       <Setter Property="Visibility" Value="Collapsed" TargetName="CheckMark"/>
      </Trigger>
      <Trigger Property="IsMouseOver" Value="true">
       <Setter Property="Fill" Value="{DynamicResource MouseOverBrush}" TargetName="Ellipse_Border"/>
      </Trigger>
      <Trigger Property="IsPressed" Value="true">
       <Setter Property="Fill" Value="{DynamicResource PressedBrush}" TargetName="Ellipse_Border"/>
       <Setter Property="Stroke" Value="{DynamicResource GlyphBrush}" TargetName="Ellipse_Border"/>
      </Trigger>
      <Trigger Property="IsEnabled" Value="false">
       <Setter Property="Fill" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Ellipse_Border"/>
       <Setter Property="Stroke" Value="#40000000" TargetName="Ellipse_Border"/>
       <Setter Property="Foreground" Value="#80000000"/>
      </Trigger>
      
     </ControlTemplate.Triggers>
    </ControlTemplate>
   </Setter.Value>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值