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

相关推荐
samble5 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
_Jonas5 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
dalong107 天前
WPF:Modbus 状态监控
wpf·modbus
He BianGu7 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
fogota10 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
fogota11 天前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota11 天前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头11 天前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
gis开发之家12 天前
Spring Boot 4 定时任务与异步线程池:从 @Scheduled 到高并发任务编排(生产级实战)
java·spring boot·后端·wpf·spring boot4
clz131452113 天前
如何设计一个优雅可靠的金融级联机接口
wpf