WPF滑块控件(Slider)的自定义样式
前言
每次开发滑块控件的样式都要花很久去读样式代码,感觉有点记不牢,所以特此备忘。
自定义滑块样式
首先创建项目,添加Slider控件。
然后获取Slider的Window样式,如下图操作。
然后弹出界面如下.我们点击确定。
点击确定后,我们的页面的Resources中,增加了一系列样式代码,而滑块代码会被修改为如下样子:
1 | <Slider HorizontalAlignment= "Left" Width= "200" VerticalAlignment= "Top" Style= "{DynamicResource SliderStyle1}" /> |
可以看到,系统为我们的Slider控件增加了样式——Style="{DynamicResource SliderStyle1}"
现在我们查看样式SliderStyle1,F12跟踪到定义。
1 2 3 4 5 6 7 8 9 10 11 12 | <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的定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <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是控制滑块背景颜色的,我们修改其背景颜色和边框颜色。
1 2 3 4 5 | <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;这两个一个是拖动前覆盖颜色,一个是拖动后覆盖颜色。
修改代码如下:
1 2 3 4 5 6 7 8 9 10 11 | <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> |
得到效果如下:
注意这里的Height一定要给值。
现在,我们设置好了轨道,可当前的滑块的颜色我们有点不太满意,所以我们再来处理下滑块。
滑块模板的模板是上方代码中粉色标记的代码——Thumb。
可以看到Thumb使用的是SliderThumbHorizontalDefault模板,所以,我们继续F12跟进SliderThumbHorizontalDefault查看它的定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <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的划线颜色如下:
1 | <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里定义的宽和高,因为不删除它们,模板里的宽高会受此限制。
删除后如下:
1 2 3 | <Track.Thumb> <Thumb x:Name= "Thumb" Focusable= "False" OverridesDefaultStyle= "True" Template= "{StaticResource SliderThumbHorizontalDefault}" VerticalAlignment= "Center" /> </Track.Thumb> |
现在我们再来修改SliderThumbHorizontalDefault模板。
在模板里找到Path,修改他的Data,之前他的Data是自己画的一个矩形,现在我们给他改为椭圆形,并且给Path重新设置宽高,如下:
1 2 3 4 5 | <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" ></EllipseGeometry> </Path.Data> </Path> |
我们得到效果如下:
可以看到,图中的滑块是个圆形,而我们需要的是一个椭圆形。
处理很简单,修改Path的Width即可,我们该为14,得到效果如下:
当然,我们既然可以通过修改样式设计椭圆形滑块,就也可以设计其他形状滑块,比如,我们修改Path如下,获得斜角四边形滑块:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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> |
效果图如下:
修改代码如下,设置三角形滑块:
1 2 3 4 5 6 7 8 9 10 11 12 | <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的背景色为透明。
1 2 3 4 5 | <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地址:https://github.com/kiba518/WpfSlider
----------------------------------------------------------------------------------------------------
注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!
https://www.cnblogs.com/kiba/p/11253686.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)