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

相关推荐
雨浓YN30 分钟前
GKMLT通讯工具箱(WPF MVVM) - 04-三菱MC通讯
wpf
不会编程的懒洋洋3 小时前
WPF XAML+布局+控件
xml·开发语言·c#·视觉检测·wpf·机器视觉·视图
雨浓YN4 小时前
GKMLT通讯工具箱(WPF MVVM) - 06-OPCUA通讯
wpf
雨浓YN5 小时前
GKMLT通讯工具箱(WPF MVVM) - 03-西门子S7通讯
wpf
雨浓YN7 小时前
GKMLT通讯工具箱(WPF MVVM) - 05-WebAPI通讯
wpf
软泡芙1 天前
【WPF 】MVVM 设计模式在 WPF 中的实战应用
设计模式·wpf
张小俊_1 天前
WPF 跨线程 UI 更新与硬编码赋值引发的 Bug 排查
c#·bug·wpf
七夜zippoe2 天前
DolphinDB在工业物联网中的优势
物联网·wpf·工业物联网·优势·dolphindb
heimeiyingwang2 天前
【架构实战】观察者模式在分布式系统中的应用
观察者模式·架构·wpf
bugcome_com2 天前
WPF + Microsoft.ToolKit.Mvvm 技术指南与实战项目
microsoft·wpf