收藏一个带动画效果的ScrollViewer以及ScrollBar的模板

原文:收藏一个带动画效果的ScrollViewer以及ScrollBar的模板

这里介绍一个带动画效果的ScrollViewer和ScrollBar,总共分为两个资源字典,直接拿来引用即可:

1 ScrollBarStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush
        x:Key="StandardBorderBrush"
        Color="DarkGray"></SolidColorBrush>
    <SolidColorBrush
        x:Key="StandardBrush"
        Color="LightGray"></SolidColorBrush>
    <SolidColorBrush
        x:Key="PressedBrush"
        Color="Gray"></SolidColorBrush>
    <SolidColorBrush
        x:Key="HoverBrush"
        Color="#fefefe"></SolidColorBrush>
    <SolidColorBrush
        x:Key="GlyphBrush"
        Color="#333333"></SolidColorBrush>
    <Style
        x:Key="VerticalScrollBarThumbStyle"
        TargetType="{x:Type Thumb}">
        <Setter
            Property="IsTabStop"
            Value="False" />
        <Setter
            Property="Focusable"
            Value="False" />
        <Setter
            Property="Margin"
            Value="1,0,1,0" />
        <Setter
            Property="BorderBrush"
            Value="{StaticResource StandardBorderBrush}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type Thumb}">
                    <Rectangle
                        Width="8"
                        Name="ellipse"
                        Stroke="{StaticResource StandardBorderBrush}"
                        Fill="{StaticResource StandardBrush}"
                        RadiusX="5"
                        RadiusY="5"></Rectangle>
                    <ControlTemplate.Triggers>
                        <Trigger
                            Property="IsMouseOver"
                            Value="true">
                            <Setter
                                TargetName="ellipse"
                                Property="Fill"
                                Value="{StaticResource HoverBrush}"></Setter>
                        </Trigger>
                        <Trigger
                            Property="IsDragging"
                            Value="True">
                            <Setter
                                TargetName="ellipse"
                                Property="Fill"
                                Value="{StaticResource PressedBrush}"></Setter>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style
        x:Key="HorizontalScrollBarThumbStyle"
        TargetType="{x:Type Thumb}">
        <Setter
            Property="IsTabStop"
            Value="False" />
        <Setter
            Property="Focusable"
            Value="False" />
        <Setter
            Property="Margin"
            Value="0,1,0,1" />
        <Setter
            Property="BorderBrush"
            Value="{StaticResource StandardBorderBrush}" />
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type Thumb}">
                    <Rectangle
                        Height="8"
                        Name="ellipse"
                        Stroke="{StaticResource StandardBorderBrush}"
                        Fill="{StaticResource StandardBrush}"
                        RadiusX="5"
                        RadiusY="5"></Rectangle>
                    <ControlTemplate.Triggers>
                        <Trigger
                            Property="IsMouseOver"
                            Value="true">
                            <Setter
                                TargetName="ellipse"
                                Property="Fill"
                                Value="{StaticResource HoverBrush}"></Setter>
                        </Trigger>
                        <Trigger
                            Property="IsDragging"
                            Value="True">
                            <Setter
                                TargetName="ellipse"
                                Property="Fill"
                                Value="{StaticResource PressedBrush}"></Setter>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style
        x:Key="LineButtonUpStyle"
        TargetType="{x:Type RepeatButton}">
        <Setter
            Property="Focusable"
            Value="False" />
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type RepeatButton}">
                    <Grid
                        Margin="1"
                        Height="18">
                        <Path
                            Stretch="None"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Name="Path"
                            Fill="{StaticResource StandardBrush}"
                            Data="M 0 8 L 8 8 L 4 0 Z"></Path>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger
                            Property="IsMouseOver"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource HoverBrush}" />
                        </Trigger>
                        <Trigger
                            Property="IsPressed"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource PressedBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style
        x:Key="LineButtonDownStyle"
        TargetType="{x:Type RepeatButton}">
        <Setter
            Property="Focusable"
            Value="False" />
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type RepeatButton}">
                    <Grid
                        Margin="1"
                        Height="18">
                        <Path
                            Stretch="None"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Name="Path"
                            Fill="{StaticResource StandardBrush}"
                            Data="M 0 0 L 4 8 L 8 0 Z">
                        </Path>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger
                            Property="IsMouseOver"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource HoverBrush}" />
                        </Trigger>
                        <Trigger
                            Property="IsPressed"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource PressedBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style
        x:Key="LineButtonLeftStyle"
        TargetType="{x:Type RepeatButton}">
        <Setter
            Property="Focusable"
            Value="False" />
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type RepeatButton}">
                    <Grid
                        Margin="1"
                        Width="18">
                        <Path
                            Stretch="None"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Name="Path"
                            Fill="{StaticResource StandardBrush}"
                            Data="M 0 0 L -8 4 L 0 8 Z"></Path>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger
                            Property="IsMouseOver"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource HoverBrush}" />
                        </Trigger>
                        <Trigger
                            Property="IsPressed"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource PressedBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style
        x:Key="LineButtonRightStyle"
        TargetType="{x:Type RepeatButton}">
        <Setter
            Property="Focusable"
            Value="False" />
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type RepeatButton}">
                    <Grid
                        Margin="1"
                        Width="18">
                        <Path
                            Stretch="None"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Name="Path"
                            Fill="{StaticResource StandardBrush}"
                            Data="M 0 0 L 8 4 L 0 8 Z">
                        </Path>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger
                            Property="IsMouseOver"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource HoverBrush}" />
                        </Trigger>
                        <Trigger
                            Property="IsPressed"
                            Value="true">
                            <Setter
                                TargetName="Path"
                                Property="Fill"
                                Value="{StaticResource PressedBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ScrollBarPageButtonStyle"
        TargetType="{x:Type RepeatButton}">
        <Setter
            Property="IsTabStop"
            Value="False" />
        <Setter
            Property="Focusable"
            Value="False" />
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type RepeatButton}">
                    <Border
                        Background="Transparent" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ControlTemplate
        x:Key="VerticalScrollBar"
        TargetType="{x:Type ScrollBar}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition
                    MaxHeight="18" />
                <RowDefinition
                    Height="*" />
                <RowDefinition
                    MaxHeight="18" />
            </Grid.RowDefinitions>
            <Grid.Background>
                <LinearGradientBrush
                    StartPoint="0,0"
                    EndPoint="0,1">
                    <GradientStop
                        Offset="0"
                        Color="#00a3d9"></GradientStop>
                    <GradientStop
                        Offset="1"
                        Color="#00a3d9"></GradientStop>
                </LinearGradientBrush>
            </Grid.Background>
            <RepeatButton
                Grid.Row="0"
                Height="18"
                Style="{StaticResource LineButtonUpStyle}"
                Command="ScrollBar.LineUpCommand">

            </RepeatButton>

            <Track
                Name="PART_Track"
                Grid.Row="1"
                IsDirectionReversed="True">
                <Track.DecreaseRepeatButton>
                    <RepeatButton
                        Command="ScrollBar.PageUpCommand"
                        Style="{StaticResource ScrollBarPageButtonStyle}">
                    </RepeatButton>
                </Track.DecreaseRepeatButton>
                <Track.Thumb>
                    <Thumb
                        Style="{StaticResource VerticalScrollBarThumbStyle}">
                    </Thumb>
                </Track.Thumb>
                <Track.IncreaseRepeatButton>
                    <RepeatButton
                        Command="ScrollBar.PageDownCommand"
                        Style="{StaticResource ScrollBarPageButtonStyle}">
                    </RepeatButton>
                </Track.IncreaseRepeatButton>
            </Track>

            <RepeatButton
                Grid.Row="2"
                Height="18"
                Style="{StaticResource LineButtonDownStyle}"
                Command="ScrollBar.LineDownCommand">
            </RepeatButton>
        </Grid>
    </ControlTemplate>
    <ControlTemplate
        x:Key="HorizontalScrollBar"
        TargetType="{x:Type ScrollBar}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition MaxWidth="18"></ColumnDefinition>
                <ColumnDefinition
                    Width="*"></ColumnDefinition>
                <ColumnDefinition
                    MaxWidth="18"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.Background>
                <LinearGradientBrush
                    StartPoint="0,0"
                    EndPoint="1,0">
                    <GradientStop
                        Offset="0"
                        Color="#4c4c4c"></GradientStop>
                    <GradientStop
                        Offset="1"
                        Color="#434343"></GradientStop>
                </LinearGradientBrush>
            </Grid.Background>
            <RepeatButton
                Grid.Column="0"
                Width="18"
                Style="{StaticResource LineButtonLeftStyle}"
                Command="ScrollBar.LineLeftCommand">
            </RepeatButton>

            <Track
                Name="PART_Track"
                Grid.Column="1"
                IsDirectionReversed="False">
                <Track.DecreaseRepeatButton>
                    <RepeatButton
                        Command="ScrollBar.PageLeftCommand"
                        Style="{StaticResource ScrollBarPageButtonStyle}">
                    </RepeatButton>
                </Track.DecreaseRepeatButton>
                <Track.Thumb>
                    <Thumb
                        Style="{StaticResource HorizontalScrollBarThumbStyle}">
                    </Thumb>
                </Track.Thumb>
                <Track.IncreaseRepeatButton>
                    <RepeatButton
                        Command="ScrollBar.PageRightCommand"
                        Style="{StaticResource ScrollBarPageButtonStyle}">
                    </RepeatButton>
                </Track.IncreaseRepeatButton>
            </Track>
            <RepeatButton
                Grid.Column="2"
                Width="18"
                Style="{StaticResource LineButtonRightStyle}"
                Command="ScrollBar.LineRightCommand">
            </RepeatButton>
        </Grid>
    </ControlTemplate>

    <Style
        x:Key="AIPAnnouncementScrollBarStyle"
        TargetType="{x:Type ScrollBar}">
        <Setter
            Property="SnapsToDevicePixels"
            Value="True" />
        <Setter
            Property="OverridesDefaultStyle"
            Value="true" />
        <Style.Triggers>
            <Trigger
                Property="Orientation"
                Value="Vertical">
                <Setter
                    Property="Width"
                    Value="18" />
                <Setter
                    Property="Height"
                    Value="Auto" />
                <Setter
                    Property="Template"
                    Value="{StaticResource VerticalScrollBar}" />
            </Trigger>
            <Trigger
                Property="Orientation"
                Value="Horizontal">
                <Setter
                    Property="Width"
                    Value="Auto" />
                <Setter
                    Property="Height"
                    Value="18" />
                <Setter
                    Property="Template"
                    Value="{StaticResource HorizontalScrollBar}" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>

  2 ScrollViewerStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/AIPAnnouncement;Component/ControlViews/Sources/ScrollBarStyle.xaml">
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="for_scrollviewer"
           TargetType="{x:Type ScrollViewer}">
        <Setter Property="BorderBrush"
                Value="LightGray"/>
        <Setter Property="BorderThickness"
                Value="0"/>
        <Setter Property="HorizontalContentAlignment"
                Value="Left"/>
        <Setter Property="HorizontalScrollBarVisibility"
                Value="Auto"/>
        <Setter Property="VerticalContentAlignment"
                Value="Top"/>
        <Setter Property="VerticalScrollBarVisibility"
                Value="Auto"/>
        <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 AIPAnnouncementScrollBarStyle}"
                                       ViewportSize="{TemplateBinding ViewportHeight}"
                                       Value="{TemplateBinding VerticalOffset}"
                                       Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
                            <ScrollBar x:Name="PART_HorizontalScrollBar"
                                       Maximum="{TemplateBinding ScrollableWidth}"
                                       Orientation="Horizontal"
                                       Style="{StaticResource AIPAnnouncementScrollBarStyle}"
                                       VerticalAlignment="Bottom"
                                       Value="{TemplateBinding HorizontalOffset}"
                                       ViewportSize="{TemplateBinding ViewportWidth}"
                                       Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="ScrollChanged">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0:0:1"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0"
                                        Duration="0:0:1"
                                        BeginTime="0:0:1"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0:0:1"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0"
                                        Duration="0:0:1"
                                        BeginTime="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseEnter"
                                      SourceName="PART_VerticalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseLeave"
                                      SourceName="PART_VerticalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_VerticalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0"
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseEnter"
                                      SourceName="PART_HorizontalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseLeave"
                                      SourceName="PART_HorizontalScrollBar">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="PART_HorizontalScrollBar"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0"
                                        Duration="0:0:1"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

  3 在主窗体中引用时,使用下面的代码即可。

  <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/AIPAnnouncement;Component/ControlViews/Sources/ScrollViewerStyle.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>

  

原文地址:https://www.cnblogs.com/lonelyxmas/p/9678830.html

时间: 2024-12-20 21:28:06

收藏一个带动画效果的ScrollViewer以及ScrollBar的模板的相关文章

一个带动画效果的颜色选择对话框控件AnimatedColorPickerDialog

android4.4的日历中选择日程显示颜色的时候有一个颜色选择对话框非常漂亮,模仿他的界面我实现了一个类似的对话框,而且带有动画效果. 代码的实现可讲的地方不多,主要是采用了和AlertDialog类似的Builder方式来创建对话框,另外当每个颜色被选择的时候有个按下效果是用纯代码实现的,还有就是可以动态的判断一排可以显示多少个颜色元素.而动画效果我们是使用属性动画实现,如果要做到兼容2.3需要使用第三方库NineOldAndroids来实现属性动画. 源码如下: 1 2 3 4 5 6 7

一个带动画效果的颜色对话框控件AnimatedColorPickerDialog

android4.4的日历中选择日程显示颜色的时候有一个颜色选择对话框非常漂亮,模仿他的界面我实现了一个类似的对话框,而且带有动画效果. 代码的实现可讲的地方不多,主要是采用了和AlertDialog类似的Builder方式来创建对话框,另外当每个颜色被选择的时候有个按下效果 是用纯代码实现的,还有就是可以动态的判断一排可以显示多少个颜色元素.而动画效果我们是使用属性动画实现,如果要做到兼容2.3需要使用第三方库 NineOldAndroids来实现属性动画. 源码如下: package com

一个带动画的dialog

根据项目需求需要在加载的时候加入一个带动画效果的进度. 最先想到的就是 自定义一个dialog 再加上一个动画就OK了. so..... public class CMYProgressDialog extends Dialog { public CMYProgressDialog(Context context) { super(context, R.style.CMYProgressDialog); setContentView(R.layout.layout_loading); getWi

写一个android带动画效果的TabHost(类似微博客户端的切换效果)

先上图: 这个Layout是: <?xml version="1.0" encoding="UTF-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" andro

一个加载时带动画效果的ListBoxItem

原文:一个加载时带动画效果的ListBoxItem 今天我们来谈一下ListBoxItem这个控件,ListBoxItem是直接从ContentControl继承而来的,所以可以添加到任何具有Content属性的控件中去,常见的ListBoxItem可以放到ListBox中,也可以放到ItemsControl中去,ListBoxItem可以横向和TreeViewItem进行比较,只不过TreeViewItem是直接从HeaderedItemsControl继承过来的,然后再继承自ItemsCon

Android带动画效果的弹窗

在网络加载数据的时候通常需要很多时间,这个时候程序里面经常需要写一个提示正在加载数据的弹窗,这篇文章用两种方式实现带动画效果的Dialog:帧动画实现和GIF动态图实现,它们都能达到动画的效果 第一种.帧动画实现 自定义一个Dialog,先看一下布局文件dialog_animation.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=

Android学习笔记(25):带动画效果的View切换ViewAnimator及其子类

ViewAnimator可以实现带动画效果的View切换,其派生的子类是一些带动画效果切换功能的组件. ViewAnimator支持的XML属性: Attribute Name Description android:animateFirstView 设置显示第一个View组件时是否使用动画 android:inAnimation 设置显示组件时使用的动画 android:outAnimation 设置隐藏组件时使用的动画 1. ViewSwitcher视图切换组件. 添加视图的方法: 由Vie

IOS的一个带动画的多项选择的控件(二)

然后我们来写:TypeSelectView 这个比较简单,我们只要只要每个TypeView的位置,然后作为自己的subview就好了 @interface TypeSelectView : UIView @property(nonatomic) BOOL bShown; @property(nonatomic, strong) TypeView* curSelectedView; -(id)initWithFrame:(CGRect)frame searchType:(int)type; @en

/*带动画效果的hover*/

1 <!DOCTYPE html> 2 /*带动画效果的hover*/ 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <title>Title</title> 7 <style> 8 .ele{ 9 background-color: #dddddd;/*带动画效果的hover*/ 10 } 11 .ele:hover{