Introduction
This article is for the design-related part. We can apply the style to the scrollbar in WPF and make it more attractive. Here, I am going to design the rounded scrollbar by applying the style. It can be used every control which has a scrollbar like ListView, DataGrid, ComboBox etc.
Step 1 - Create a new WPF Project
Step 2 - Add ListView in MainWindow
- <Window x:Class="WpfRoundedScrollbar.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:WpfRoundedScrollbar"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
- <Grid>
- <ListView x:Name="listRound" Margin="10"/>
- </Grid>
- </Window>
Step 3 - Edit Style of ListView.
Here, <ListView.Resources> Contain the styles of the internal controls. We have to apply the style to the ScrollBar. So, add TargetType="{x:Type ScrollBar}"
- <ListView x:Name="listRound" Margin="10">
- <ListView.Resources>
- <Style TargetType="{x:Type ScrollBar}">
- <Setter Property="Background" Value="#505050"/>
- <Setter Property="BorderBrush" Value="#505050"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ScrollBar}">
- <Grid x:Name="Bg" SnapsToDevicePixels="true">
- <Grid.RowDefinitions>
- <RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/>
- <RowDefinition Height="0.00001*"/>
- <RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/>
- </Grid.RowDefinitions>
- <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Row="1" CornerRadius="10"/>
- <!--<RepeatButton x:Name="PART_LineUpButton" Command="{x:Static ScrollBar.LineUpCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Style="{StaticResource ScrollBarButton}" Margin="0,0,0,-20">
- <Path x:Name="ArrowTop" Data="M 0,4 C0,4 0,6 0,6 0,6 3.5,2.5 3.5,2.5 3.5,2.5 7,6 7,6 7,6 7,4 7,4 7,4 3.5,0.5 3.5,0.5 3.5,0.5 0,4 0,4 z" Fill="{StaticResource ScrollBar.Static.Glyph}" Margin="3,4,3,3" Stretch="Uniform"/>
- </RepeatButton>-->
- <Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="1">
- <Track.DecreaseRepeatButton>
- <RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource RepeatButtonTransparent}" HorizontalAlignment="Left" Width="17"/>
- </Track.DecreaseRepeatButton>
- <Track.IncreaseRepeatButton>
- <RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource RepeatButtonTransparent}"/>
- </Track.IncreaseRepeatButton>
- <Track.Thumb>
- <Thumb Style="{StaticResource ScrollBarThumbVertical}" Margin="2"/>
- </Track.Thumb>
- </Track>
- <!--<RepeatButton x:Name="PART_LineDownButton" Command="{x:Static ScrollBar.LineDownCommand}" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="2" Style="{StaticResource ScrollBarButton}" Margin="0,-20,0,0">
- <Path x:Name="ArrowBottom" Data="M 0,2.5 C0,2.5 0,0.5 0,0.5 0,0.5 3.5,4 3.5,4 3.5,4 7,0.5 7,0.5 7,0.5 7,2.5 7,2.5 7,2.5 3.5,6 3.5,6 3.5,6 0,2.5 0,2.5 z" Fill="{StaticResource ScrollBar.Static.Glyph}" Margin="3,4,3,3" Stretch="Uniform"/>
- </RepeatButton>-->
- </Grid>
-
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </ListView.Resources>
- </ListView>
Step 4 - Also, create the style for the RepeatButton and Thumb of the ScrollBar.
For that we need to add the <Window.Resources> to add the multiple styles
- <Window.Resources>
- <Style x:Key="RepeatButtonTransparent" TargetType="{x:Type RepeatButton}">
- <Setter Property="OverridesDefaultStyle" Value="true"/>
- <Setter Property="Background" Value="#505050"/>
- <Setter Property="Focusable" Value="false"/>
- <Setter Property="IsTabStop" Value="false"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type RepeatButton}">
- <Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" CornerRadius="10"/>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
-
- <Style x:Key="ScrollBarThumbVertical" TargetType="{x:Type Thumb}">
- <Setter Property="OverridesDefaultStyle" Value="true"/>
- <!--<Setter Property="Margin" Value="3"/>-->
-
- <Setter Property="IsTabStop" Value="false"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type Thumb}">
- <Border x:Name="rectangle" Background="Black" Height="{TemplateBinding Height}" SnapsToDevicePixels="True" Width="{TemplateBinding Width}" CornerRadius="8"/>
- <ControlTemplate.Triggers>
- <Trigger Property="IsMouseOver" Value="true">
- <Setter Property="Background" TargetName="rectangle" Value="Black"/>
- </Trigger>
- <Trigger Property="IsDragging" Value="true">
- <Setter Property="Background" TargetName="rectangle" Value="Black"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
Step 5 - Add the items in ListView
Now, we are adding the States of India as Items in ListView via backend code.
- public MainWindow()
- {
- InitializeComponent();
- listRound.Items.Add("Andhra Pradesh");
- listRound.Items.Add("Arunachal Pradesh");
- listRound.Items.Add("Assam");
- listRound.Items.Add("Bihar");
- listRound.Items.Add("Chhattisgarh");
- listRound.Items.Add("Goa");
- listRound.Items.Add("Gujarat");
- listRound.Items.Add("Haryana");
- listRound.Items.Add("Himachal Pradesh");
- listRound.Items.Add("Jharkhand");
- listRound.Items.Add("Karnataka");
- listRound.Items.Add("Kerala");
- listRound.Items.Add("Madhya Pradesh");
- listRound.Items.Add("Maharashtra");
- listRound.Items.Add("Manipur");
- listRound.Items.Add("Meghalaya");
- listRound.Items.Add("Mizoram");
- listRound.Items.Add("Nagaland");
- listRound.Items.Add("Odisha");
- listRound.Items.Add("Punjab");
- listRound.Items.Add("Rajasthan");
- listRound.Items.Add("Sikkim");
- listRound.Items.Add("Tamil Nadu");
- listRound.Items.Add("Telangana");
- listRound.Items.Add("Tripura");
- listRound.Items.Add("Uttar Pradesh");
- listRound.Items.Add("Uttarakhand");
- listRound.Items.Add("West Bengal");
- }
Step 6 - Run the Project and See the Output
Here, you can see the rounded ScrollBar in ListView as below
You can change the colour according to your project theme by using,
- <Window.Resources>
- <Style x:Key="RepeatButtonTransparent" TargetType="{x:Type RepeatButton}">
- <Setter Property="OverridesDefaultStyle" Value="true"/>
- <Setter Property="Background" Value="LightGray"/>
- <Setter Property="Focusable" Value="false"/>
- <Setter Property="IsTabStop" Value="false"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type RepeatButton}">
- <Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" CornerRadius="10"/>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
-
- <Style x:Key="ScrollBarThumbVertical" TargetType="{x:Type Thumb}">
- <Setter Property="OverridesDefaultStyle" Value="true"/>
- <!--<Setter Property="Margin" Value="3"/>-->
-
- <Setter Property="IsTabStop" Value="false"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type Thumb}">
- <Border x:Name="rectangle" Background="Gray" Height="{TemplateBinding Height}" SnapsToDevicePixels="True" Width="{TemplateBinding Width}" CornerRadius="8"/>
- <ControlTemplate.Triggers>
- <Trigger Property="IsMouseOver" Value="true">
- <Setter Property="Background" TargetName="rectangle" Value="Gray"/>
- </Trigger>
- <Trigger Property="IsDragging" Value="true">
- <Setter Property="Background" TargetName="rectangle" Value="Gray"/>
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
You can apply the same in ComboBox,
- <ComboBox>
- <ComboBox.Resources>
- <Style TargetType="{x:Type ScrollBar}">
- <Setter Property="Background" Value="LightGray"/>
- <Setter Property="BorderBrush" Value="LightGray"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ScrollBar}">
- <Grid x:Name="Bg" SnapsToDevicePixels="true">
- <Grid.RowDefinitions>
- <RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/>
- <RowDefinition Height="0.00001*"/>
- <RowDefinition MaxHeight="{DynamicResource {x:Static SystemParameters.VerticalScrollBarButtonHeightKey}}"/>
- </Grid.RowDefinitions>
- <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Row="1" CornerRadius="10"/>
- <Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}" Grid.Row="1">
- <Track.DecreaseRepeatButton>
- <RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource RepeatButtonTransparent}" HorizontalAlignment="Left" Width="17"/>
- </Track.DecreaseRepeatButton>
- <Track.IncreaseRepeatButton>
- <RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource RepeatButtonTransparent}"/>
- </Track.IncreaseRepeatButton>
- <Track.Thumb>
- <Thumb Style="{StaticResource ScrollBarThumbVertical}" Margin="3"/>
- </Track.Thumb>
- </Track>
- </Grid>
-
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </ComboBox.Resources>
- </ComboBox>