关闭

WPF ScrollViewer 样式

2271人阅读 评论(0) 收藏 举报
分类:
[code=csharp]
  <!--所有页面滚动条样式-->
        <Style TargetType="{x:Type ScrollViewer}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="CanContentScroll" Value="True"></Setter>
            <Style.Resources>
                <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#D6D4D4" />
                <SolidColorBrush x:Key="NormalBrush" Color="Transparent" />
                <SolidColorBrush x:Key="NormalBorderBrush" Color="#D6D4D4" />
                <SolidColorBrush x:Key="HorizontalNormalBrush" Color="#D6D4D4" />
                <SolidColorBrush x:Key="HorizontalNormalBorderBrush" Color="#D6D4D4" />
                <!--方向箭头颜色-->
                <SolidColorBrush x:Key="GlyphBrush" Color="Black" />
                <LinearGradientBrush x:Key="PressedBrush"
            StartPoint="0,0" EndPoint="0,1">
                    <GradientBrush.GradientStops>
                        <GradientStopCollection>
                            <GradientStop Color="#BBB" Offset="0.0"/>
                            <GradientStop Color="#EEE" Offset="0.1"/>
                            <GradientStop Color="#EEE" Offset="0.9"/>
                            <GradientStop Color="#FFF" Offset="1.0"/>
                        </GradientStopCollection>
                    </GradientBrush.GradientStops>
                </LinearGradientBrush>
                <!--方向箭头按钮-->
                <Style x:Key="ScrollBarLineButton" TargetType="{x:Type RepeatButton}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Setter Property="Focusable" Value="false"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type RepeatButton}">
                                <Border Name="Border" Margin="1" Background="{StaticResource NormalBrush}">
                                    <Path HorizontalAlignment="Center" VerticalAlignment="Center" Fill="{StaticResource GlyphBrush}"
            Data="{Binding Path=Content,
                RelativeSource={RelativeSource TemplatedParent}}" />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <!--选择箭头时的背景色-->
                                    <Trigger Property="IsPressed" Value="true">
                                        <Setter TargetName="Border" Property="Background"
                                Value="#6A6A6A" />
                                    </Trigger>
                                    <!--鼠标悬浮时 设置透明度-->
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Opacity" TargetName="Border" Value="0.8"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsEnabled" Value="false">
                                        <Setter Property="Foreground"
                                Value="{StaticResource DisabledForegroundBrush}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <!-- 滚动条显示部分样式-->
                <Style x:Key="ScrollBarPageButton" TargetType="{x:Type RepeatButton}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <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>
                <!--滚动条样式-->
                <Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Setter Property="IsTabStop" Value="false"/>
                    <Setter Property="Width" Value="8"/>
                    <Setter Property="Focusable" Value="false"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Thumb}">
                                <Border x:Name="border"  Background="#D6D4D4" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="0" />
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" TargetName="border" Value="#6A6A6A"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </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>
                        <Border  Grid.RowSpan="3" CornerRadius="2"  Background="Transparent"  BorderThickness="0"
	  BorderBrush="#D6D4D4" />
                        <RepeatButton  Grid.Row="0" Style="{StaticResource ScrollBarLineButton}"
      Height="18" Command="ScrollBar.LineUpCommand" Content="M 0 4 L 8 4 L 4 0 Z" />
                        <Track Name="PART_Track" Grid.Row="1" IsDirectionReversed="true">
                            <Track.DecreaseRepeatButton>
                                <RepeatButton Style="{StaticResource ScrollBarPageButton}"/>
                            </Track.DecreaseRepeatButton>
                            <Track.Thumb>
                                <Thumb Style="{StaticResource ScrollBarThumb}" Margin="1,0,1,0" Background="{StaticResource HorizontalNormalBrush}"
          BorderBrush="{StaticResource HorizontalNormalBorderBrush}" />
                            </Track.Thumb>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton
          Style="{StaticResource ScrollBarPageButton}"
          Command="ScrollBar.PageDownCommand" />
                            </Track.IncreaseRepeatButton>
                        </Track>
                        <RepeatButton Grid.Row="3" Style="{StaticResource ScrollBarLineButton}" Height="18" Command="ScrollBar.PageDownCommand"  Content="M 0 0 L 4 4 L 8 0 Z"/>
                    </Grid>
                </ControlTemplate>

                <Style x:Key="HScrollBarThumb" TargetType="{x:Type Thumb}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Setter Property="IsTabStop" Value="false"/>
                    <Setter Property="Height" Value="9"/>
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="Focusable" Value="false"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Thumb}">
                                <Border  x:Name="border" CornerRadius="0" Background="#D6D4D4"
                             BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" />
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Background" TargetName="border" Value="#6A6A6A"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

                <ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}">
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition MaxWidth="18"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition MaxWidth="18"/>
                        </Grid.ColumnDefinitions>
                        <Border  Grid.ColumnSpan="3"  CornerRadius="2" Background="Transparent"  BorderThickness="0"
	  BorderBrush="#D6D4D4" />
                        <RepeatButton   Grid.Column="0" Style="{StaticResource ScrollBarLineButton}"  Width="18"  Command="ScrollBar.LineLeftCommand" Content="M 4 0 L 4 8 L 0 4 Z" />
                        <Track Name="PART_Track" Grid.Column="1" IsDirectionReversed="False">
                            <Track.DecreaseRepeatButton>
                                <RepeatButton Style="{StaticResource ScrollBarPageButton}"
 />
                            </Track.DecreaseRepeatButton>
                            <Track.Thumb>
                                <Thumb Style="{StaticResource HScrollBarThumb}" Margin="0,1,0,1" Background="{StaticResource NormalBrush}"
          BorderBrush="{StaticResource NormalBorderBrush}" />
                            </Track.Thumb>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton  Style="{StaticResource ScrollBarPageButton}" />
                            </Track.IncreaseRepeatButton>
                        </Track>
                        <RepeatButton Grid.Column="2" Style="{StaticResource ScrollBarLineButton}" Width="18" Command="ScrollBar.PageRightCommand" 
 Content="M 0 0 L 4 4 L 0 8 Z"/>
                    </Grid>
                </ControlTemplate>
                <!--滚动条整体样式-->
                <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}">
                    <Setter Property="SnapsToDevicePixels" Value="True"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Style.Triggers>
                        <Trigger Property="Orientation" Value="Horizontal">
                            <Setter Property="Width" Value="Auto"/>
                            <Setter Property="Height" Value="10" />
                            <Setter Property="Template"
                        Value="{StaticResource HorizontalScrollBar}" />
                        </Trigger>
                        <Trigger Property="Orientation" Value="Vertical">
                            <Setter Property="Width" Value="10"/>
                            <Setter Property="Height" Value="Auto" />
                            <Setter Property="Template"
                        Value="{StaticResource VerticalScrollBar}" />
                        </Trigger>
                    </Style.Triggers></Style>   </Style.Resources> </Style>
[/code]

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:103199次
    • 积分:1375
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:0篇
    • 译文:1篇
    • 评论:23条
    文章分类
    最新评论