WPF 自定义滚动条控件ScrollViewer(二)

WPF 自定义滚动条控件ScrollViewer(二)

一.创建一个资源字典:ScrollViewDictionary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ControlTemplate x:Key="MyScrollViewer" TargetType="{x:Type ScrollViewer}">
        <!--View区域背景色-->
        <Grid x:Name="Grid" Background="{TemplateBinding Background}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Rectangle x:Name="Corner" Grid.Column="1" Fill="White" Grid.Row="1"/>
            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0" Margin="{TemplateBinding Padding}" Grid.Row="0"/>
            <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}" Style="{DynamicResource MyScrollBarStyle}"/>
            <ScrollBar x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow" Grid.Column="0" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"  Style="{DynamicResource MyScrollBarStyle}"/>
        </Grid>
    </ControlTemplate>


    <SolidColorBrush x:Key="ScrollBarDisabledBackground" Color="#F4F4F4"/>

    <Style x:Key="VerticalScrollBarPageButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--滚动条颜色、圆角等设置-->
    <Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <!--滚动条颜色和圆角设置-->
                    <Rectangle Name="thumbRect" Fill="#686868" RadiusX="3" RadiusY="3"/>
                    <!--鼠标拉动滚动条时的颜色-->
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Fill" Value="CornflowerBlue" TargetName="thumbRect" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="HorizontalScrollBarPageButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Rectangle Fill="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MyScrollBarStyle" TargetType="{x:Type ScrollBar}">
        <Setter Property="Background" Value="AliceBlue"/>
        <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
        <!--滚动条宽度-->
        <Setter Property="Width" Value="8"/>
        <Setter Property="MinWidth" Value="6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollBar}">
                    <!--滚动条背景色-->
                    <Grid x:Name="Bg" Background="#DDDDDD" SnapsToDevicePixels="true" Width="8">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Track x:Name="PART_Track" IsDirectionReversed="true" IsEnabled="{TemplateBinding IsMouseOver}">
                            <Track.DecreaseRepeatButton>
                                <RepeatButton Command="{x:Static ScrollBar.PageUpCommand}" Style="{StaticResource VerticalScrollBarPageButton}"/>
                            </Track.DecreaseRepeatButton>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton Command="{x:Static ScrollBar.PageDownCommand}" Style="{StaticResource VerticalScrollBarPageButton}"/>
                            </Track.IncreaseRepeatButton>
                            <Track.Thumb>
                                <Thumb Style="{StaticResource ScrollBarThumb}"/>
                            </Track.Thumb>
                        </Track>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="Bg" Value="{StaticResource ScrollBarDisabledBackground}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="Orientation" Value="Horizontal">
                <Setter Property="Width" Value="Auto"/>
                <Setter Property="MinWidth" Value="0"/>
                <Setter Property="Height" Value="6"/>
                <Setter Property="MinHeight" Value="6"/>
                <Setter Property="Background" Value="AliceBlue"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ScrollBar}">
                            <Grid x:Name="Bg" Background="Red" SnapsToDevicePixels="true">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Track x:Name="PART_Track"  IsEnabled="{TemplateBinding IsMouseOver}">
                                    <Track.DecreaseRepeatButton>
                                        <RepeatButton Command="{x:Static ScrollBar.PageLeftCommand}" Style="{StaticResource HorizontalScrollBarPageButton}"/>
                                    </Track.DecreaseRepeatButton>
                                    <Track.IncreaseRepeatButton>
                                        <RepeatButton Command="{x:Static ScrollBar.PageRightCommand}" Style="{StaticResource HorizontalScrollBarPageButton}"/>
                                    </Track.IncreaseRepeatButton>
                                    <Track.Thumb>
                                        <Thumb Style="{StaticResource ScrollBarThumb}" />
                                    </Track.Thumb>
                                </Track>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Background" TargetName="Bg" Value="{StaticResource ScrollBarDisabledBackground}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

二.在窗体中引用资源字典中的样式:

<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ScrollViewDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

三.主窗体代码:

<Grid>
        <ScrollViewer Template="{StaticResource MyScrollViewer}" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top" Grid.ColumnSpan="2" x:Name="scrList" Margin="0" VerticalScrollBarVisibility="Auto" Height="350" Width="250">
            <StackPanel Orientation="Vertical" Name="layerList" ScrollViewer.VerticalScrollBarVisibility="Visible" Width="250" >
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据一"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据二"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据三"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据四"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据五"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据六"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据七"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据八"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据九"></Button>
                <Button Height="50" Width="230" HorizontalAlignment="Left" Content="数据十"></Button>
            </StackPanel>
        </ScrollViewer>
    </Grid>

四.运行效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中可以通过自定义样式来改变滚动条的外观。以下是实现步骤: 1. 在 XAML 中定义一个样式,设置 TargetType 为 ScrollBar。 ```xaml <Window.Resources> <Style x:Key="ScrollBarStyle" TargetType="ScrollBar"> <!-- 自定义样式 --> </Style> </Window.Resources> ``` 2. 在样式中定义滚动条控件模板,可以使用一系列 WPF 控件和属性来自定义滚动条的外观。 ```xaml <Window.Resources> <Style x:Key="ScrollBarStyle" TargetType="ScrollBar"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ScrollBar"> <Grid> <Border Background="Gray" CornerRadius="5"/> <RepeatButton x:Name="PART_LineUpButton" Style="{StaticResource ScrollBarButtonStyle}" Content="↑"/> <RepeatButton x:Name="PART_LineDownButton" Style="{StaticResource ScrollBarButtonStyle}" Content="↓"/> <Thumb x:Name="PART_Thumb" Style="{StaticResource ScrollBarThumbStyle}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> ``` 在上面的模板中,我们使用了 Grid、Border、RepeatButton 和 Thumb 等控件来构建滚动条的外观。其中 RepeatButton 和 Thumb 也可以通过自定义样式来改变其外观。 3. 在需要使用滚动条控件上,使用 ScrollViewer 控件,并设置其 VerticalScrollBarStyle 或 HorizontalScrollBarStyle 属性为自定义的样式。 ```xaml <ScrollViewer VerticalScrollBarStyle="{StaticResource ScrollBarStyle}"> <!-- 内容 --> </ScrollViewer> ``` 通过以上步骤,就可以自定义滚动条的外观了。需要注意的是,不同的控件可能需要不同的样式,需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值