Search code examples
wpfxamlwpf-style

How to apply ScrollBar and ListBoxItem styles on only one ListBox?


I have a style that targets any ScrollBar by the Key in the app.xaml:

x:Key="{x:Type ScrollBar}" 

but the problem is that it's applied on any scroll bar in the application, the same happens for the ListBoxItem. How to apply it on only one ListBox, so I can use a string-based key, for example, I'm looking for something like this AP in the ListBox

ScrollBar.Style="{StaticResource ScrollBarStyle}"

Solution

  • Try to add an explicit Style to <ListBox.Resources>:

    <ListBox>
        <ListBox.Resources>
            <Style TargetType="ScrollBar" BasedOn="{StaticResource ScrollBarStyle}" />
        </ListBox.Resources>
        ...
    </ListBox>