WPF 滑块控件(Slider)的自定义样式

202 阅读5分钟

以下是原文的完整输出,代码部分已格式化处理:

前言

每次开发滑块控件的样式都要花很久去读样式代码,感觉有点记不牢,所以特此备忘。

自定义滑块样式

首先创建项目,添加Slider控件。

然后获取Slider的Window样式,如下图操作。

然后弹出界面如下.我们点击确定。

点击确定后,我们的页面的Resources中,增加了一系列样式代码,而滑块代码会被修改为如下样子:

<Slider HorizontalAlignment="Left" Width="200" VerticalAlignment="Top" 
        Style="{DynamicResource SliderStyle1}"/>

可以看到,系统为我们的Slider控件增加了样式——Style="{DynamicResource SliderStyle1}"

现在我们查看样式SliderStyle1,F12跟踪到定义。

<Style x:Key="SliderStyle1" TargetType="{x:Type Slider}">
  <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
  <Setter Property="Background" Value="Transparent"/>
  <Setter Property="BorderBrush" Value="Transparent"/>
  <Setter Property="Foreground" Value="{StaticResource SliderThumb.Static.Foreground}"/>
  <Setter Property="Template" Value="{StaticResource SliderHorizontal}"/>
  <Style.Triggers>
    <Trigger Property="Orientation" Value="Vertical">
      <Setter Property="Template" Value="{StaticResource SliderVertical}"/>
    </Trigger>
  </Style.Triggers>
</Style>

上述代码中我们可以看发现Slider使用的模板是SliderHorizontal,但当他的排列方向为Vertical时,则使用SliderVertical模板。

因为Slider控件默认是横向布局,所以我们先修改SliderHorizontal模板,对Slider进行下美化。

同样,我们继续F12跟进SliderHorizontal的定义。

<ControlTemplate x:Key="SliderHorizontal" TargetType="{x:Type Slider}">
  <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          Background="{TemplateBinding Background}" 
          SnapsToDevicePixels="True">
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
        <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <TickBar x:Name="TopTick" Fill="{TemplateBinding Foreground}" Height="4" 
               Margin="0,0,0,2" Placement="Top" Grid.Row="0" Visibility="Collapsed"/>
      <TickBar x:Name="BottomTick" Fill="{TemplateBinding Foreground}" Height="4" 
               Margin="0,2,0,0" Placement="Bottom" Grid.Row="2" Visibility="Collapsed"/>
      <Border x:Name="TrackBackground" BorderBrush="{StaticResource SliderThumb.Track.Border}" 
              BorderThickness="1" Background="{StaticResource SliderThumb.Track.Background}" 
              Height="4.0" Margin="5,0" Grid.Row="1" VerticalAlignment="center">
        <Canvas Margin="-6,-1">
          <Rectangle x:Name="PART_SelectionRange" 
                     Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" 
                     Height="4.0" Visibility="Hidden"/>
        </Canvas>
      </Border>
      <Track x:Name="PART_Track" Grid.Row="1">
        <Track.DecreaseRepeatButton>
          <RepeatButton Command="{x:Static Slider.DecreaseLarge}" 
                        Style="{StaticResource RepeatButtonTransparent}"/>
        </Track.DecreaseRepeatButton>
        <Track.IncreaseRepeatButton>
          <RepeatButton Command="{x:Static Slider.IncreaseLarge}" 
                        Style="{StaticResource RepeatButtonTransparent}"/>
        </Track.IncreaseRepeatButton>
        <Track.Thumb>
          <Thumb x:Name="Thumb" Focusable="False" Height="18" 
                 OverridesDefaultStyle="True" 
                 Template="{StaticResource SliderThumbHorizontalDefault}" 
                 VerticalAlignment="Center" Width="11"/>
        </Track.Thumb>
      </Track>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="TickPlacement" Value="TopLeft">
      <Setter Property="Visibility" TargetName="TopTick" Value="Visible"/>
      <Setter Property="Template" TargetName="Thumb" Value="{StaticResource SliderThumbHorizontalTop}"/>
      <Setter Property="Margin" TargetName="TrackBackground" Value="5,2,5,0"/>
    </Trigger>
    <Trigger Property="TickPlacement" Value="BottomRight">
      <Setter Property="Visibility" TargetName="BottomTick" Value="Visible"/>
      <Setter Property="Template" TargetName="Thumb" Value="{StaticResource SliderThumbHorizontalBottom}"/>
      <Setter Property="Margin" TargetName="TrackBackground" Value="5,0,5,2"/>
    </Trigger>
    <Trigger Property="TickPlacement" Value="Both">
      <Setter Property="Visibility" TargetName="TopTick" Value="Visible"/>
      <Setter Property="Visibility" TargetName="BottomTick" Value="Visible"/>
    </Trigger>
    <Trigger Property="IsSelectionRangeEnabled" Value="true">
      <Setter Property="Visibility" TargetName="PART_SelectionRange" Value="Visible"/>
    </Trigger>
    <Trigger Property="IsKeyboardFocused" Value="true">
      <Setter Property="Foreground" TargetName="Thumb" Value="Blue"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

SliderHorizontal模板的定义比较多,这里直接定义到重点内容——轨道。

首先定位到代码**【Border x:Name="TrackBackground"】**,这里的TrackBackground是控制滑块背景颜色的,我们修改其背景颜色和边框颜色。

<Border x:Name="TrackBackground" BorderBrush="Red" BorderThickness="1" 
        Background="Yellow" Height="4.0" Margin="5,0" Grid.Row="1" 
        VerticalAlignment="center">
  <Canvas Margin="-6,-1">
    <Rectangle x:Name="PART_SelectionRange" 
               Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" 
               Height="4.0" Visibility="Hidden"/>
  </Canvas>
</Border>

得到效果如下:

但我们有时候需要拖动前后颜色不一样,此时就靠背景修改就不够了。

在SliderHorizontal模板中找到DecreaseRepeatButton和IncreaseRepeatButton;这两个一个是拖动前覆盖颜色,一个是拖动后覆盖颜色。

修改代码如下:

<Track x:Name="PART_Track" Grid.Row="1">
  <Track.DecreaseRepeatButton>
    <RepeatButton Height="4" Background="Gray" 
                  Command="{x:Static Slider.DecreaseLarge}" 
                  Style="{StaticResource RepeatButtonTransparent}"/>
  </Track.DecreaseRepeatButton>
  <Track.IncreaseRepeatButton>
    <RepeatButton Height="4" Background="Green" 
                  Command="{x:Static Slider.IncreaseLarge}" 
                  Style="{StaticResource RepeatButtonTransparent}"/>
  </Track.IncreaseRepeatButton>
  <Track.Thumb>
    <Thumb x:Name="Thumb" Focusable="False" Height="18" 
           OverridesDefaultStyle="True" 
           Template="{StaticResource SliderThumbHorizontalDefault}" 
           VerticalAlignment="Center" Width="11"/>
  </Track.Thumb>
</Track>

得到效果如下:

img

注意这里的Height一定要给值。

现在,我们设置好了轨道,可当前的滑块的颜色我们有点不太满意,所以我们再来处理下滑块。

滑块模板的模板是上方代码中粉色标记的代码——Thumb。

可以看到Thumb使用的是SliderThumbHorizontalDefault模板,所以,我们继续F12跟进SliderThumbHorizontalDefault查看它的定义。

<ControlTemplate x:Key="SliderThumbHorizontalDefault" TargetType="{x:Type Thumb}">
  <Grid HorizontalAlignment="Center" UseLayoutRounding="True" VerticalAlignment="Center">
    <Path x:Name="grip" 
          Data="M 0,0 C0,0 11,0 11,0 11,0 11,18 11,18 11,18 0,18 0,18 0,18 0,0 0,0 z" 
          Fill="{StaticResource SliderThumb.Static.Background}" 
          Stretch="Fill" SnapsToDevicePixels="True" 
          Stroke="{StaticResource SliderThumb.Static.Border}" 
          StrokeThickness="1" UseLayoutRounding="True" 
          VerticalAlignment="Center"/>
  </Grid>
  <ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="true">
      <Setter Property="Fill" TargetName="grip" 
              Value="{StaticResource SliderThumb.MouseOver.Background}"/>
      <Setter Property="Stroke" TargetName="grip" 
              Value="{StaticResource SliderThumb.MouseOver.Border}"/>
    </Trigger>
    <Trigger Property="IsDragging" Value="true">
      <Setter Property="Fill" TargetName="grip" 
              Value="{StaticResource SliderThumb.Pressed.Background}"/>
      <Setter Property="Stroke" TargetName="grip" 
              Value="{StaticResource SliderThumb.Pressed.Border}"/>
    </Trigger>
    <Trigger Property="IsEnabled" Value="false">
      <Setter Property="Fill" TargetName="grip" 
              Value="{StaticResource SliderThumb.Disabled.Background}"/>
      <Setter Property="Stroke" TargetName="grip" 
              Value="{StaticResource SliderThumb.Disabled.Border}"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

从上述代码中可以看到,滑块定义很简单,布局就是一个Grid里放了一个Path,事件响应只有3个。

下面为修改Path的Fill填充色和Stroke的划线颜色如下:

<Path x:Name="grip" 
      Data="M 0,0 C0,0 11,0 11,0 11,0 11,18 11,18 11,18 0,18 0,18 0,18 0,0 0,0 z" 
      Fill="Red" Stretch="Fill" SnapsToDevicePixels="True" 
      Stroke="Blue" StrokeThickness="1" UseLayoutRounding="True" 
      VerticalAlignment="Center"/>

得到效果如下:

现在,我们觉得矩形的滑块不好看,需要用椭圆形的滑块,那么,我们再来处理下滑块。

首先删除Thumb里定义的宽和高,因为不删除它们,模板里的宽高会受此限制。

删除后如下:

<Track.Thumb>
  <Thumb x:Name="Thumb" Focusable="False"  
         OverridesDefaultStyle="True" 
         Template="{StaticResource SliderThumbHorizontalDefault}" 
         VerticalAlignment="Center"/>
</Track.Thumb>

现在我们再来修改SliderThumbHorizontalDefault模板。

在模板里找到Path,修改他的Data,之前他的Data是自己画的一个矩形,现在我们给他改为椭圆形,并且给Path重新设置宽高,如下:

<Path x:Name="grip" Width="20" Height="20" Fill="Red" 
      Stretch="Fill" SnapsToDevicePixels="True" Stroke="Blue" 
      StrokeThickness="1" UseLayoutRounding="True" 
      VerticalAlignment="Center">
  <Path.Data>
    <EllipseGeometry Center="10,10" RadiusX="10" RadiusY="10"/>
  </Path.Data>
</Path>

我们得到效果如下:

img

可以看到,图中的滑块是个圆形,而我们需要的是一个椭圆形。

处理很简单,修改Path的Width即可,我们该为14,得到效果如下:

img

当然,我们既然可以通过修改样式设计椭圆形滑块,就也可以设计其他形状滑块,比如,我们修改Path如下,获得斜角四边形滑块:

<Path x:Name="grip" Width="14" Height="20" Fill="Red" 
      Stretch="Fill" SnapsToDevicePixels="True" Stroke="Blue" 
      StrokeThickness="1" UseLayoutRounding="True" 
      VerticalAlignment="Center">
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigure StartPoint="0,0" IsClosed="True">
          <LineSegment Point="0,0"/>
          <LineSegment Point="110,0"/>
          <LineSegment Point="70,40"/>
          <LineSegment Point="-40,40"/>
        </PathFigure>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>

效果图如下:

修改代码如下,设置三角形滑块:

<Path x:Name="grip" Width="14" Height="20" Fill="Red" 
      Stretch="Fill" SnapsToDevicePixels="True" Stroke="Blue" 
      StrokeThickness="1" UseLayoutRounding="True" 
      VerticalAlignment="Center">
  <Path.Data>
    <PathGeometry>
      <PathGeometry.Figures>
        <PathFigure StartPoint="0,0" IsClosed="True">
          <LineSegment Point="30,0"/>
          <LineSegment Point="15,100"/>
        </PathFigure>
      </PathGeometry.Figures>
    </PathGeometry>
  </Path.Data>
</Path>

效果图如下:

上述代码设置的都是水平方向的滑块样式,垂直方向的滑块样式设置同理,只要从模板SliderVertical开始,以此处理修改即可。

特殊图形我们看到,在图形后面还有一个小背景色在显示,解决方法:修改TrackBackground的背景色为透明。

<Border x:Name="TrackBackground" 
        BorderBrush="{StaticResource SliderThumb.Track.Border}" 
        BorderThickness="0" 
        Background="Transparent" 
        Height="4.0" Margin="5,0" Grid.Row="1" 
        VerticalAlignment="center">
  <Canvas Margin="-6,-1">
    <Rectangle x:Name="PART_SelectionRange" 
               Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" 
               Height="4.0" Visibility="Hidden"/>
  </Canvas>
</Border>

到此WPF滑块控件(Slider)的自定义样式就已经讲解完成了。

代码已经传到Github上了,欢迎大家下载。

Github地址:github.com/kiba518/Wpf…

关键词:WPF、Slider、自定义样式、滑块控件、模板、轨道、Thumb、几何图形、样式触发器

最后

如果你觉得这篇文章对你有帮助,不妨点个赞支持一下!你的支持是我继续分享知识的动力。如果有任何疑问或需要进一步的帮助,欢迎随时留言。

也可以加入微信公众号 [DotNet技术匠] 社区,与其他热爱技术的同行一起交流心得,共同成长!

优秀是一种习惯,欢迎大家留言学习!

作者:kiba518

出处:cnblogs.com/kiba/p/11253686.html

声明:网络内容,仅供学习,尊重版权,侵权速删,歉意致谢!