自定义滚动条(Custom ScrollBar)

1、先看效果

图片



2、本文背景

设计师给的效果图中,滚动条的样式好好看呀,可是手上现有的控件库很差改呀,那我本身从新实现样式吧。express

3、代码实现

小编使用.Net Core 3.1建立的WPF工程,建立名称为“ScrollBar”的解决方案后,不添加任何用户控件,直接在MainWindow.xaml文件中开干。app

下面是上图显示的窗体标题及滚动视图代码:ide

<Grid Background="#FF222222">
       <Grid.RowDefinitions>
           <RowDefinition Height="60"/>
           <RowDefinition Height="*"/>
       </Grid.RowDefinitions>
       <Grid Grid.Row="0">
           <TextBlock Margin="10" Text="Custom ScrollBar" Foreground="#FFEEEEEE" FontSize="33" FontFamily="Script MT Bold" VerticalAlignment="Center"/>
       </Grid>
       <ScrollViewer Grid.Row="1">
           <Grid Height="1000"/>
       </ScrollViewer>
   </Grid>

下面是上面未添加样式时代码的效果:测试



除了标题还看得过去,滚动条丑到爆有木有?下面开始添加样式,即覆盖滚动条默认的样式:spa

<Window.Resources>
       <ResourceDictionary>
           <Style x:Key="ScrollThumbs" TargetType="{x:Type Thumb}">
               <Setter Property="Template">
                   <Setter.Value>
                       <ControlTemplate>
                           <Grid x:Name="Grid">
                               <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto"
                                          Height="Auto" Fill="Transparent"/>
                               <Border x:Name="Rectangle1" CornerRadius="10 0 0 10" HorizontalAlignment="Stretch"
                                       VerticalAlignment="Stretch" Width="Auto" Height="Auto"
                                       Background="{TemplateBinding Background}"/>
                           </Grid>
                           <ControlTemplate.Triggers>
                               <Trigger Property="Tag" Value="Horizontal">
                                   <Setter TargetName="Rectangle1" Property="Width" Value="Auto"/>
                                   <Setter TargetName="Rectangle1" Property="Height" Value="7"/>
                               </Trigger>
                           </ControlTemplate.Triggers>
                       </ControlTemplate>
                   </Setter.Value>
               </Setter>
           </Style>
           
           <!--SCROLLBARS-->
           <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
               <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
               <Setter Property="Foreground" Value="#AAA8341A"/>
               <Setter Property="Background" Value="DarkGray"/>
               <Setter Property="Width" Value="10"/>
               <Setter Property="Template">
                   <Setter.Value>
                       <ControlTemplate TargetType="{x:Type ScrollBar}">
                           <Grid x:Name="GridRoot" Width="12" Background="{x:Null}">
                               <Track x:Name="PART_Track" Grid.Row="0" IsDirectionReversed="True" Focusable="False">
                                   <Track.Thumb>
                                       <Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}"
                                              Style="{DynamicResource ScrollThumbs}"/>
                                   </Track.Thumb>
                                   <Track.IncreaseRepeatButton>
                                       <RepeatButton x:Name="PageUp" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="True"/>
                                   </Track.IncreaseRepeatButton>
                                   <Track.DecreaseRepeatButton>
                                       <RepeatButton x:Name="PageDown" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="True"/>
                                   </Track.DecreaseRepeatButton>
                               </Track>
                           </Grid>

                           <ControlTemplate.Triggers>
                               <Trigger SourceName="Thumb" Property="IsMouseOver" Value="True">
                                   <Setter Value="{DynamicResource ButtonSelectBrush}"
                                           TargetName="Thumb" Property="Background"/>
                               </Trigger>
                               <Trigger SourceName="Thumb" Property="IsDragging" Value="True">
                                   <Setter Value="{DynamicResource DarkBrush}"
                                           TargetName="Thumb" Property="Background"/>
                               </Trigger>
                               
                               <Trigger Property="IsEnabled" Value="False">
                                   <Setter TargetName="Thumb" Property="Visibility" Value="Collapsed"/>
                               </Trigger>
                               <Trigger Property="Orientation" Value="Horizontal">
                                   <Setter TargetName="GridRoot" Property="LayoutTransform">
                                       <Setter.Value>
                                           <RotateTransform Angle="-90"/>
                                       </Setter.Value>
                                   </Setter>
                                   <Setter TargetName="PART_Track" Property="LayoutTransform">
                                       <Setter.Value>
                                           <RotateTransform Angle="-90"/>
                                       </Setter.Value>
                                   </Setter>
                                   <Setter Property="Width" Value="Auto"/>
                                   <Setter Property="Height" Value="12"/>
                                   <Setter TargetName="Thumb" Property="Tag" Value="Horizontal"/>
                                   <Setter TargetName="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand"/>
                                   <Setter TargetName="PageUp" Property="Command" Value="ScrollBar.PageRightCommand"/>
                               </Trigger>
                           </ControlTemplate.Triggers>
                       </ControlTemplate>
                   </Setter.Value>
               </Setter>
           </Style>
       </ResourceDictionary>
   </Window.Resources>

下面是整个MainWindow.xaml的代码,您直接copy到您的测试工程中就能够用了:设计

<Window x:Class="ScrollBar.MainWindow"
       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/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:local="clr-namespace:ScrollBar"
       mc:Ignorable="d"
       Title="MainWindow" Height="450" Width="300" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None">
   <Window.Resources>
       <ResourceDictionary>
           <Style x:Key="ScrollThumbs" TargetType="{x:Type Thumb}">
               <Setter Property="Template">
                   <Setter.Value>
                       <ControlTemplate>
                           <Grid x:Name="Grid">
                               <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto"
                                          Height="Auto" Fill="Transparent"/>
                               <Border x:Name="Rectangle1" CornerRadius="10 0 0 10" HorizontalAlignment="Stretch"
                                       VerticalAlignment="Stretch" Width="Auto" Height="Auto"
                                       Background="{TemplateBinding Background}"/>
                           </Grid>
                           <ControlTemplate.Triggers>
                               <Trigger Property="Tag" Value="Horizontal">
                                   <Setter TargetName="Rectangle1" Property="Width" Value="Auto"/>
                                   <Setter TargetName="Rectangle1" Property="Height" Value="7"/>
                               </Trigger>
                           </ControlTemplate.Triggers>
                       </ControlTemplate>
                   </Setter.Value>
               </Setter>
           </Style>
           
           <!--SCROLLBARS-->
           <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
               <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
               <Setter Property="Foreground" Value="#AAA8341A"/>
               <Setter Property="Background" Value="DarkGray"/>
               <Setter Property="Width" Value="10"/>
               <Setter Property="Template">
                   <Setter.Value>
                       <ControlTemplate TargetType="{x:Type ScrollBar}">
                           <Grid x:Name="GridRoot" Width="12" Background="{x:Null}">
                               <Track x:Name="PART_Track" Grid.Row="0" IsDirectionReversed="True" Focusable="False">
                                   <Track.Thumb>
                                       <Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}"
                                              Style="{DynamicResource ScrollThumbs}"/>
                                   </Track.Thumb>
                                   <Track.IncreaseRepeatButton>
                                       <RepeatButton x:Name="PageUp" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="True"/>
                                   </Track.IncreaseRepeatButton>
                                   <Track.DecreaseRepeatButton>
                                       <RepeatButton x:Name="PageDown" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="True"/>
                                   </Track.DecreaseRepeatButton>
                               </Track>
                           </Grid>

                           <ControlTemplate.Triggers>
                               <Trigger SourceName="Thumb" Property="IsMouseOver" Value="True">
                                   <Setter Value="{DynamicResource ButtonSelectBrush}"
                                           TargetName="Thumb" Property="Background"/>
                               </Trigger>
                               <Trigger SourceName="Thumb" Property="IsDragging" Value="True">
                                   <Setter Value="{DynamicResource DarkBrush}"
                                           TargetName="Thumb" Property="Background"/>
                               </Trigger>
                               
                               <Trigger Property="IsEnabled" Value="False">
                                   <Setter TargetName="Thumb" Property="Visibility" Value="Collapsed"/>
                               </Trigger>
                               <Trigger Property="Orientation" Value="Horizontal">
                                   <Setter TargetName="GridRoot" Property="LayoutTransform">
                                       <Setter.Value>
                                           <RotateTransform Angle="-90"/>
                                       </Setter.Value>
                                   </Setter>
                                   <Setter TargetName="PART_Track" Property="LayoutTransform">
                                       <Setter.Value>
                                           <RotateTransform Angle="-90"/>
                                       </Setter.Value>
                                   </Setter>
                                   <Setter Property="Width" Value="Auto"/>
                                   <Setter Property="Height" Value="12"/>
                                   <Setter TargetName="Thumb" Property="Tag" Value="Horizontal"/>
                                   <Setter TargetName="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand"/>
                                   <Setter TargetName="PageUp" Property="Command" Value="ScrollBar.PageRightCommand"/>
                               </Trigger>
                           </ControlTemplate.Triggers>
                       </ControlTemplate>
                   </Setter.Value>
               </Setter>
           </Style>
       </ResourceDictionary>
   </Window.Resources>
   <Grid Background="#FF222222">
       <Grid.RowDefinitions>
           <RowDefinition Height="60"/>
           <RowDefinition Height="*"/>
       </Grid.RowDefinitions>
       <Grid Grid.Row="0">
           <TextBlock Margin="10" Text="Custom ScrollBar" Foreground="#FFEEEEEE" FontSize="33" FontFamily="Script MT Bold" VerticalAlignment="Center"/>
       </Grid>
       <ScrollViewer Grid.Row="1">
           <Grid Height="1000"/>
       </ScrollViewer>
   </Grid>
</Window>

4、文章参考

参考:
Design com WPF :     https://www.youtube.com/watch?v=aQeXth-1B0I&t=350scode

5、代码下载

文章中代码已经所有贴出,自定义滚动条,主要是改变滚动条的Track样式,也即Track的Thumb、IncreaseRepeatButton、DecreaseRepeatButton三个成员的样式,您get到了吗?orm

相关文章
相关标签/搜索