前言
每次開發滑塊控件的樣式都要花很久去讀樣式代碼,感覺有點記不牢,所以特此備忘。
自定義滑塊樣式
首先創建項目,添加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>
得到效果如下:

注意這里的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"></EllipseGeometry> </Path.Data> </Path>
我們得到效果如下:

可以看到,圖中的滑塊是個圓形,而我們需要的是一個橢圓形。
處理很簡單,修改Path的Width即可,我們該為14,得到效果如下:

當然,我們既然可以通過修改樣式設計橢圓形滑塊,就也可以設計其他形狀滑塊,比如,我們修改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地址:https://github.com/kiba518/WpfSlider
----------------------------------------------------------------------------------------------------
注:此文章為原創,任何形式的轉載都請聯系作者獲得授權並注明出處!
若您覺得這篇文章還不錯,請點擊下方的【推薦】,非常感謝!
https://www.cnblogs.com/kiba/p/11253686.html

