我在 resourceDictionary 中设置滚动条的样式,但没有给它一个键值:
<Style TargetType="{x:Type ScrollBar}">
...
</Style>
Bur 由于某种原因,只有 Scrollbar 类型的组件受样式影响。不是 ListView 组件的滚动条!
我认为所有滚动条都具有相同的样式,因为我没有在样式定义中使用任何键值!
有任何想法吗?
最佳答案
默认的 WPF 行为是您的隐式 ScrollBar 样式将应用于 ListBox 中的滚动条。如果这在您的应用程序中没有发生,那么就会有一些东西覆盖了这个默认行为。您是否有应用于 ListBox 的模板?
我的用于证明默认样式行为的测试应用程序如下:
<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>
关于wpf - [WPF] : Styling a scrollbar, 但ListView 滚动条不受样式影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2729537/