WPF TreeView自带自定义滚动条

放在TreeView.Resources中:

<Style TargetType="ScrollBar">

<Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>

<Setter Property="Stylus.IsFlicksEnabled" Value="false"/>

<Setter Property="Width" Value="12"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type ScrollBar}">

<Grid x:Name="Bg" SnapsToDevicePixels="true">

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<!-- 定义滚动条的各个部分,如按钮和轨道 -->

<RepeatButton Grid.Row="0" Style="{StaticResource ScrollBarButtonStyle}" Command="{x:Static ScrollBar.LineUpCommand}" >

<Image Source="/Image/UpperIcon.png"/>

</RepeatButton>

<Track x:Name="PART_Track" Grid.Row="1" IsDirectionReversed="true">

<Track.DecreaseRepeatButton>

<RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource ScrollBarPageButtonStyle}"/>

</Track.DecreaseRepeatButton>

<Track.Thumb>

<Thumb Style="{StaticResource ScrollBarThumbStyle}"/>

</Track.Thumb>

<Track.IncreaseRepeatButton>

<RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource ScrollBarPageButtonStyle}"/>

</Track.IncreaseRepeatButton>

</Track>

<RepeatButton Grid.Row="2" Style="{StaticResource ScrollBarButtonStyle}" Command="{x:Static ScrollBar.LineDownCommand}" >

<Image Source="/Image/BelowIcon.png"/>

</RepeatButton>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

剩下的放上面资源中:

<!-- 按钮样式定义 -->

<Style x:Key="ScrollBarButtonStyle" TargetType="{x:Type RepeatButton}">

<Setter Property="BorderThickness" Value="0"/>

</Style>

<!-- 页面按钮样式定义 -->

<Style x:Key="ScrollBarPageButtonStyle" TargetType="{x:Type RepeatButton}">

<Setter Property="BorderThickness" Value="0"/>

<Setter Property="Background" Value="Transparent"/>

</Style>

<!-- 拇指样式定义 -->

<Style x:Key="ScrollBarThumbStyle" TargetType="{x:Type Thumb}">

<Setter Property="Background" Value="LightGray"/>

<Setter Property="Width" Value="12"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type Thumb}">

<Border Background="{TemplateBinding Background}" CornerRadius="6">

<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

最后记得给TreeView设置高度,不然滚动条出不来

相关推荐
SamChan905 小时前
PDF翻译服务端到端基准测试:4款主流方案的吞吐量、延迟、内存占用对比
服务器·网络·python·ai·pdf·wpf
bugcome_com5 小时前
Avalonia 控件模板实战:从 WPF 迁移自定义 Button 样式的完整指南
wpf
七夜zippoe5 小时前
DolphinDB 故障排查实战:系统、数据库与集群常见问题诊断
数据库·wpf·集群·常见问题·故障排查·dolphindb
dalong101 天前
WPF:箭头绘制程序
wpf·自定义绘制
张工在路上2 天前
WPF 布局基础概述
大数据·hadoop·wpf
迪飞特科技3 天前
分布式事务下 Spring 声明式事务失效的 3 种修复方案
分布式·spring·wpf
2601_962174176 天前
Redis 简介
数据库·redis·wpf
SamChan907 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan907 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience7 天前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf