How to apply style for ScrollBar placed in template of another control


Hello. Today I’ll show how to apply style for ScrollBar in WPF for control that internally uses it.

Consider an example where you have ListView that contains items in it:

clip_image001

If you decrease size of this window, ListView will show items and scroll that allows you to look for items out of the current view:

clip_image002

What if you want to apply certain style for this ScrollBar? You just need to overwrite style for ScrollBar in ListView resources collection:

<ListView> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListViewItem Content="Item"/> <ListView.Resources> <Style TargetType="ScrollBar"> <Setter Property="Width" Value="2"/> <Setter Property="Template"> <Setter.Value> ..Template for scroll bar </Setter.Value> </Setter> </Style> </ListView.Resources> </ListView>

For the sake of clarity I haven’t included template for ScrollBar but you’ve got an idea.

That is one way I apply styles for controls that are used internally in other controls. You could also overwrite whole template for ListView but in my case I just needed to apply different template for ScrollBar without changes to ListView.

If you have other ways to apply styles for internal controls just post comment below. Bye J