Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[WPF]: Styling a scrollbar, but the ListView scrollbar is not affected by the style

I am styling a scrollbar in a resourceDictionary without giving it a key value:

<Style TargetType="{x:Type ScrollBar}">
      ...
</Style>

Bur for some reason only a component of the type Scrollbar are affected by the style. Not the ListView component's scrollbar!

I would Think that all scrollbars would have the same style since I am not using any key value in the style definition!

Any ideas?

like image 634
code-zoop Avatar asked Nov 14 '22 11:11

code-zoop


1 Answers

The default WPF behavior is that your implicit ScrollBar style would apply to the scrollbars in the ListBox. If this is not occurring in your application then there is something overriding this default behavior. Do you have a Template applied to the ListBox?

My test app to prove out the default styling behavior is below:

<Window x:Class="TestScrollBarStyle.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Window.Resources>        
    <Style TargetType="ScrollBar">
        <Setter Property="Background" Value="Red" />
    </Style>        
</Window.Resources>   

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <ScrollViewer x:Name="scroll">
        <Rectangle Height="200" />
    </ScrollViewer>

    <ListBox Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Visible">
        <ListBoxItem>Test 1</ListBoxItem>
        <ListBoxItem>Test 2</ListBoxItem>
        <ListBoxItem>Test 3</ListBoxItem>
        <ListBoxItem>Test 4</ListBoxItem>
        <ListBoxItem>Test 5</ListBoxItem>
        <ListBoxItem>Test 6</ListBoxItem>
        <ListBoxItem>Test 7</ListBoxItem>
        <ListBoxItem>Test 8</ListBoxItem>
        <ListBoxItem>Test 9</ListBoxItem>
        <ListBoxItem>Test 10</ListBoxItem>
        <ListBoxItem>Test 11</ListBoxItem>
    </ListBox>

</Grid>

like image 173
Tony Borres Avatar answered Jan 12 '23 16:01

Tony Borres