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

相关推荐
LateFrames5 小时前
以小白视角尝试 WPF / WinUI3 / MAUI / MAUI Blazor 构建 Windows 桌面程序
windows·wpf·maui·mauiblazor·winui3
偶尔的鼠标人18 小时前
Avalonia/WPF 打开子窗口,并且跨页面传值
c#·wpf·mvvm·avalonia
玖笙&18 小时前
✨WPF编程进阶【6.1】:图形原则(附源码)
c++·c#·wpf·visual studio
lixy57919 小时前
WPF检测网络状态切换
wpf
纸照片19 小时前
WPF中为Button设置IsMouseOver和IsPressed事件中改变背景颜色不起作用
c#·.net·wpf
Aevget19 小时前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(四)
ui·.net·wpf·devexpress·wpf控件
Z_W_H_20 小时前
【ArcGISProSDK】刷新按钮样式
wpf·arcgisprosdk
rollingman1 天前
WPF ListView 列表
wpf
Hare_bai2 天前
WPF的MVVM模式核心架构与实现细节
ui·架构·c#·wpf·交互·xaml·mvvm
张人玉2 天前
WPF 静态样式与动态样式的定义及使用详解
ui·c#·wpf