XAML ScrollViewer ControlTemplate中的SmallChange无效

Mik*_*ham 6 wpf xaml

我已经创建了一个基本的Windows桌面WPF应用程序.在MainWindow中,我添加了以下内容作为窗口的主体:

    <ScrollViewer Template="{DynamicResource ScrollViewerControlTemplate1}">
        <ScrollViewer.Resources>
            <ControlTemplate x:Key="ScrollViewerControlTemplate1" TargetType="{x:Type ScrollViewer}">
                <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="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" 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}" SmallChange="40000"/>
                    <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}"/>
                </Grid>
            </ControlTemplate>
        </ScrollViewer.Resources>
        <Grid Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="300"/>
                <RowDefinition Height="300"/>
                <RowDefinition Height="300"/>
                <RowDefinition Height="300"/>
                <RowDefinition Height="300"/>
            </Grid.RowDefinitions>

            <Border Grid.Row="0" Background="SteelBlue"/>
            <Border Grid.Row="1" Background="Peru"/>
            <Border Grid.Row="2" Background="Goldenrod"/>
            <Border Grid.Row="3" Background="Tomato"/>
            <Border Grid.Row="4" Background="IndianRed"/>
        </Grid>

    </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

你会注意到在PART_VerticalScrollbar上,我设置了SmallChange ="40000"(一个任意大的数字).然而,当我单击滚动条上的向上/向下箭头时,它会像我将SmallChange设置为任何内容之前那样进行相同的非常小的更改.

我已经阅读了很多次文档,并且无法弄清楚为什么这对ScrollViewer滚动的数量没有任何影响.有任何想法吗?

请注意,我可以更改ScrollBar模板并将这些按钮调用的命令更改为Scrollbar.PageUpCommand而不是Scrollbar.LineUpCommand,但最终我希望能够更好地控制滚动而不是整页.

Ste*_*nds 0

我怀疑您的自定义控件模板没有被使用,因为您引用它的方式。我会将 XAML 更改为:

<ScrollViewer>
    <ScrollViewer.Template>
        <ControlTemplate TargetType="{x:Type ScrollViewer}">
            <Grid ...>
                ...
            </Grid>
        </ControlTemplate>
    </ScrollViewer.Template>

    ... ScrollViewer content goes here

</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

基本上,直接设置控件的模板,而不是使用动态资源。