wpf listbox 修改滚动条样式
时间: 2023-09-28 08:04:11 浏览: 232
wpf滚动条样式
4星 · 用户满意度95%
可以通过自定义ListBox的样式来修改滚动条的样式。以下是一些基本步骤:
1. 创建ListBox控件的样式
```xml
<Style x:Key="CustomListBoxStyle" TargetType="{x:Type ListBox}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
<ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
2. 创建ScrollBar控件的样式
```xml
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="18"/>
<RowDefinition Height="0.00001*"/>
<RowDefinition MaxHeight="18"/>
</Grid.RowDefinitions>
<Border Grid.RowSpan="3" CornerRadius="2" Background="#F0F0F0"/>
<RepeatButton Grid.Row="0" Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.LineUpCommand"/>
<Track Grid.Row="1" Name="PART_Track">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.PageUpCommand"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.PageDownCommand"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb Style="{DynamicResource ScrollBarThumbStyle}" Margin="1,0,1,0"/>
</Track.Thumb>
</Track>
<RepeatButton Grid.Row="2" Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.LineDownCommand"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
3. 在ListBox中应用自定义样式
```xml
<ListBox Style="{StaticResource CustomListBoxStyle}">
<!-- Add items here -->
</ListBox>
```
4. 修改ScrollBar的样式
你可以修改ScrollBar的样式,使其更加符合你的需求。例如,你可以修改ScrollBar的颜色、大小等属性。以下是一个示例:
```xml
<Style TargetType="{x:Type ScrollBar}">
<Setter Property="Width" Value="20"/>
<Setter Property="Foreground" Value="#FF00FF"/>
<Setter Property="Background" Value="#FFFFFF"/>
<Setter Property="BorderBrush" Value="#000000"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="18"/>
<RowDefinition Height="0.00001*"/>
<RowDefinition MaxHeight="18"/>
</Grid.RowDefinitions>
<Border Grid.RowSpan="3" CornerRadius="2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
<RepeatButton Grid.Row="0" Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.LineUpCommand"/>
<Track Grid.Row="1" Name="PART_Track">
<Track.DecreaseRepeatButton>
<RepeatButton Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.PageUpCommand"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.PageDownCommand"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb Style="{DynamicResource ScrollBarThumbStyle}" Margin="1,0,1,0"/>
</Track.Thumb>
</Track>
<RepeatButton Grid.Row="2" Style="{DynamicResource ScrollBarButtonStyle}" Command="ScrollBar.LineDownCommand"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
```
阅读全文