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设置高度,不然滚动条出不来

相关推荐
LateFrames3 小时前
520 - 如何说晚安 (WPF)
c#·wpf·浪漫·ui体验
heimeiyingwang14 小时前
【架构实战】日志体系ELK:集中化日志管理实践
elk·架构·wpf
CPU不够了15 小时前
WPF 多选下拉+搜索过滤_wpf下拉选项增加搜索
wpf
FuckPatience15 小时前
WPF 列表控件自动拉伸子元素的宽度
wpf
LCG元15 小时前
【Go后端开发】从 0 到生产级:高性能分布式网关全实现 + 接口限流熔断降级实战
分布式·golang·wpf
枫叶林FYL1 天前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
她说彩礼65万1 天前
WPF 多值转换器
wpf
无心水2 天前
【分布式利器:金融级】金融级分布式架构开源框架全景解读
人工智能·分布式·金融·架构·开源·wpf·金融级框架
她说彩礼65万2 天前
WPF 转换器
wpf
WPF工业上位机2 天前
匠心研智造,同心赴新程-WPF硬件通讯之串口&Socket
wpf