• WPF 自定义滚动条样式


    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
        <ResourceDictionary.MergedDictionaries>
    
        </ResourceDictionary.MergedDictionaries>
    
    
        <!-- 滚动条滑块 -->
        <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}">
                        <Grid>
                            <!--滚动条滑块颜色-->
                            <Border Background="#FF898989" CornerRadius="3"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <!-- 滚动条滑块上下空白区域 -->
        <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="Opacity" Value="0.2"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <Rectangle Fill="{TemplateBinding Background}" 
                                   Width="{TemplateBinding Width}" 
                                   Height="{TemplateBinding Height}"/>
                    </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="Opacity" Value="0.2"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <Rectangle Fill="{TemplateBinding Background}" 
                                   Width="{TemplateBinding Width}" 
                                   Height="{TemplateBinding Height}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <!-- 滚动条上下按钮 -->
        <Style x:Key="VerticalScrollBarPageButton2" 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="Opacity"  Value="0.2"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <Rectangle Fill="#90000000" Width="0" Height="0"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <!-- 滚动条完整 -->
        <Style x:Key="for_scrollbar" TargetType="{x:Type ScrollBar}">
            <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="false"/>
            <Setter Property="Margin" Value="0,2,0,2"/>
            <Setter Property="Width" Value="6"/>
            <Setter Property="MinWidth" Value="6"/>
            <Setter Property="Opacity" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollBar}">
                        <Grid x:Name="Bg" SnapsToDevicePixels="true">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto"></RowDefinition>
                                <RowDefinition Height="*"></RowDefinition>
                                <RowDefinition Height="auto"></RowDefinition>
                            </Grid.RowDefinitions>
                            <RepeatButton  Grid.Row="0" Style="{DynamicResource VerticalScrollBarPageButton2}" Command="{x:Static ScrollBar.PageUpCommand}"/>
                            <Track x:Name="PART_Track"   Grid.Row="1" IsEnabled="{TemplateBinding IsMouseOver}" IsDirectionReversed="true">
                                <Track.DecreaseRepeatButton>
                                    <RepeatButton Style="{DynamicResource VerticalScrollBarPageButton}" Command="{x:Static ScrollBar.PageUpCommand}"/>
                                </Track.DecreaseRepeatButton>
                                <Track.IncreaseRepeatButton>
                                    <RepeatButton Style="{DynamicResource VerticalScrollBarPageButton}" Command="{x:Static ScrollBar.PageDownCommand}"/>
                                </Track.IncreaseRepeatButton>
                                <Track.Thumb>
                                    <Thumb Style="{DynamicResource ScrollBarThumb}"/>
                                </Track.Thumb>
                            </Track>
                            <RepeatButton Grid.Row="2" Style="{DynamicResource VerticalScrollBarPageButton2}" Command="{x:Static ScrollBar.PageDownCommand}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Orientation" Value="Horizontal">
                    <Setter Property="Background"  Value="Transparent"/>
                    <Setter Property="Margin" Value="2,0,2,0"/>
                    <Setter Property="Height"   Value="6"/>
                    <Setter Property="MinHeight"  Value="6"/>
                    <Setter Property="Width"    Value="Auto"/>
                    <Setter Property="Opacity" Value="1"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ScrollBar}">
                                <Grid x:Name="Bg" SnapsToDevicePixels="true">
                                    <Track x:Name="PART_Track" IsEnabled="{TemplateBinding IsMouseOver}">
                                        <Track.DecreaseRepeatButton>
                                            <RepeatButton Style="{StaticResource HorizontalScrollBarPageButton}"  Command="{x:Static ScrollBar.PageLeftCommand}"/>
                                        </Track.DecreaseRepeatButton>
                                        <Track.IncreaseRepeatButton>
                                            <RepeatButton Style="{StaticResource HorizontalScrollBarPageButton}"  Command="{x:Static ScrollBar.PageRightCommand}"/>
                                        </Track.IncreaseRepeatButton>
                                        <Track.Thumb>
                                            <Thumb Style="{StaticResource ScrollBarThumb}"/>
                                        </Track.Thumb>
                                    </Track>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    
        <!-- 滚动条 -->
        <Style x:Key="ScrollViewerBaseStyle" TargetType="{x:Type ScrollViewer}" BasedOn="{StaticResource BaseStyle}" >
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                SnapsToDevicePixels="True">
                            <Grid Background="{TemplateBinding Background}">
                                <ScrollContentPresenter 
                                    Cursor="{TemplateBinding Cursor}" 
                                    Margin="{TemplateBinding Padding}" 
                                    ContentTemplate="{TemplateBinding ContentTemplate}" />
                                <ScrollBar x:Name="PART_VerticalScrollBar" 
                                           HorizontalAlignment="Right" 
                                           Maximum="{TemplateBinding ScrollableHeight}"
                                           Orientation="Vertical" 
                                           Style="{StaticResource for_scrollbar}" 
                                           ViewportSize="{TemplateBinding ViewportHeight}"
                                           Value="{TemplateBinding VerticalOffset}" 
                                           Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}">
                                </ScrollBar>
                                <ScrollBar x:Name="PART_HorizontalScrollBar"
                                           Maximum="{TemplateBinding ScrollableWidth}"
                                           Orientation="Horizontal"
                                           Style="{StaticResource for_scrollbar}"
                                           VerticalAlignment="Bottom"
                                           Value="{TemplateBinding HorizontalOffset}"
                                           ViewportSize="{TemplateBinding ViewportWidth}"
                                           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}">
                                </ScrollBar>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <EventTrigger RoutedEvent="MouseEnter">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_VerticalScrollBar"
                                            Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.1"/>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_HorizontalScrollBar"
                                            Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.1"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="MouseLeave">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_VerticalScrollBar"
                                            Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.1"/>
                                        <DoubleAnimation
                                            Storyboard.TargetName="PART_HorizontalScrollBar"
                                            Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.1"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
    </ResourceDictionary>
    
  • 相关阅读:
    bzoj1059: [ZJOI2007]矩阵游戏
    NEW
    bzoj2438: [中山市选2011]杀人游戏
    bzoj4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
    【高精度】模板 (C++)
    【BZOJ4025】二分图 LCT
    读入/输出模板
    一些 Markdown 语法
    题解 P3732 [HAOI2017]供给侧改革
    题解 CF1598A Computer Game
  • 原文地址:https://www.cnblogs.com/flamegreat/p/14622964.html
Copyright © 2020-2023  润新知