WPF ScrollViewer 自定义样式

<UserControl x:Class="WpfListViewGotCell.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Height="300" Width="300">
    <UserControl.Resources>
        <ImageBrush x:Key="ScrollUp" ImageSource="Resources/Images/slide_up.png"></ImageBrush>

        <ImageBrush x:Key="ScrollDown" ImageSource="Resources/Images/slide_down.png"></ImageBrush>

        <ImageBrush x:Key="Scrollbarbutton" ImageSource="Resources/ProfileDialogImages/scrollbarbutton.png"></ImageBrush>
        <ImageBrush x:Key="ScrollbarBG" ImageSource="Resources/ProfileDialogImages/scrollbar_BG.png"></ImageBrush>


        <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
            <!--<Setter Property="SelectionMode" Value="Multiple"></Setter>-->
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type ListBoxItem}" >
                        <Setter Property="Margin" Value="2" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <ContentPresenter></ContentPresenter>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

        <!--scrllbar style-->
        <Style x:Key="ScrollBarUpButtonStyle" TargetType="{x:Type RepeatButton}">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Margin" Value="-1,1,0,0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <Grid Margin="0" Width="9" Height="10" >
                            <Rectangle  Width="10" Height="8" Name="BorderUp" StrokeThickness="1" Stroke="{StaticResource ScrollUp}"
                     Fill="{StaticResource ScrollUp}"></Rectangle >
                            <!--<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>-->
                        </Grid>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ScrollBarDownButtonStyle" TargetType="{x:Type RepeatButton}">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RepeatButton}">
                        <Grid Margin="0" Width="9" Height="10">
                            <Rectangle Margin="0,0,0,0" Name="BorderDown" Width="10" Height="8" StrokeThickness="1" Stroke="{StaticResource ScrollDown}"
                     Fill="{StaticResource ScrollDown}"></Rectangle >
                            <!--<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>-->
                        </Grid>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


        <Style x:Key="ScrollBarPageButtonStyle" TargetType="{x:Type RepeatButton}">
            <Setter Property="IsTabStop" Value="False"/>
            <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 Background="{StaticResource ScrollbarBG}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ScrollBarThumbStyle" TargetType="{x:Type Thumb}">
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Margin" Value="0,0,0,0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Grid Width="9">
                            <Rectangle Name="Slide" Width="9" Height="52" Fill="{StaticResource Scrollbarbutton}"></Rectangle>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ControlTemplate x:Key="VerticalScrollBar_WITH_PART_SO_WORKS" TargetType="{x:Type ScrollBar}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="10"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="10"/>
                </Grid.RowDefinitions>
                <Border Width="9" Background="{StaticResource ScrollbarBG}" Opacity="1" Grid.RowSpan="3" CornerRadius="1"/>
                <RepeatButton Grid.Row="0" Style="{StaticResource ScrollBarUpButtonStyle}" Command="ScrollBar.LineUpCommand">
                    <Path Fill="{StaticResource ScrollUp}" Data="M 0 4 L 8 4 L 4 0 Z"></Path>
                </RepeatButton>
                <Track Name="PART_Track" Grid.Row="1" IsDirectionReversed="True" ViewportSize="0" Width="9">
                    <Track.DecreaseRepeatButton>
                        <RepeatButton Command="ScrollBar.PageUpCommand" Style="{StaticResource ScrollBarPageButtonStyle}">
                        </RepeatButton>
                    </Track.DecreaseRepeatButton>
                    <Track.Thumb>
                        <Thumb Style="{StaticResource ScrollBarThumbStyle}">
                           
                        </Thumb>
                    </Track.Thumb>
                    <Track.IncreaseRepeatButton>
                        <RepeatButton Command="ScrollBar.PageDownCommand" Style="{StaticResource ScrollBarPageButtonStyle}">
                        </RepeatButton>
                    </Track.IncreaseRepeatButton>
                    <Track.Resources>
                        <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarButtonHeightKey}">
                            104
                        </sys:Double>
                    </Track.Resources>
                </Track>

                <RepeatButton Grid.Row="3" Style="{StaticResource ScrollBarDownButtonStyle}" Command="ScrollBar.LineDownCommand">
                    <Path Fill="{StaticResource ScrollDown}" Data="M 0 4 L 8 4 L 4 0 Z"></Path>
                </RepeatButton>
            </Grid>
        </ControlTemplate>

        <Style x:Key="MyMinBlockListScrollBar" TargetType="{x:Type ScrollBar}">
            <Style.Triggers>
                <Trigger Property="Orientation" Value="Vertical">
                    <Setter Property="MinWidth" Value="10"/>
                    <Setter Property="Width" Value="40"/>
                    <Setter Property="Height" Value="Auto" />
                    <Setter Property="Template" Value="{StaticResource VerticalScrollBar_WITH_PART_SO_WORKS}" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style  TargetType="{x:Type ScrollViewer}">

            <Setter Property="SnapsToDevicePixels" Value="True"/>

            <Setter Property=" CanContentScroll" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <ScrollContentPresenter Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Center"/>
                            <ScrollBar Name="PART_VerticalScrollBar" Grid.Column="1"  Value="{TemplateBinding VerticalOffset}"
                                       HorizontalAlignment="Center"
                                       Maximum="{TemplateBinding ScrollableHeight}"
                                       Style="{StaticResource MyMinBlockListScrollBar}" BorderThickness="2">
                            </ScrollBar>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <ListBox Name="list" Style="{StaticResource CheckBoxListStyle}" SelectionMode="Single" BorderThickness="0" Width="176" Height="180">
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>

         <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>

        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>

       <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
        <ListBoxItem>ssssssss</ListBoxItem>
    </ListBox>

</UserControl>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值