WPF如何影响滚动视图的滚动量

时间:2018-10-05 12:18:28

标签: c# wpf

我有一个TabControl,其中有一个TabPanel,放在ScrollViewer内。

<Style x:Key="TabCtrl" TargetType="{x:Type TabControl}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Padding" Value="2,0,0,0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <RepeatButton
                        x:Name="ScrolltoLeft_Btn"
                        Grid.Row="0"
                        Grid.Column="0"
                        VerticalAlignment="Top"
                        Command="{x:Static ScrollBar.LineLeftCommand}"
                        CommandTarget="{Binding ElementName=sv}"
                        Style="{StaticResource ScrolltoLeft}" />

                    <ScrollViewer
                        x:Name="sv"
                        Grid.Row="0"
                        Grid.Column="1"

                        HorizontalScrollBarVisibility="Hidden"
                        VerticalScrollBarVisibility="Disabled">

                        <TabPanel
                            x:Name="HeaderPanel"
                            Panel.ZIndex="1"
                            IsItemsHost="true"
                            KeyboardNavigation.TabIndex="1" />
                    </ScrollViewer>

                    <ContentPresenter
                        x:Name="PART_SelectedContentHost"
                        Grid.Row="1"
                        Grid.ColumnSpan="2"
                        Grid.Column="0"
                        Margin="{TemplateBinding Padding}"
                        ContentSource="SelectedContent"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />

                    <RepeatButton
                        x:Name="ScrolltoRight_Btn"
                        Grid.Row="0"
                        Grid.Column="2"

                        VerticalAlignment="Top"
                        Command="{x:Static ScrollBar.LineRightCommand}"
                        CommandTarget="{Binding ElementName=sv}"
                        Style="{StaticResource ScrolltoRight}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

当我按下重复按钮时,滚动量很小,只有一个TabItem宽度的一小部分。

我希望这个更大。我希望(至少)滚动TabItem的宽度。

我已经使用了CanContentScroll的{​​{1}}属性,但这并没有改变。

我还尝试更改水平滚动条上的ScrollViewer属性,但这也没有影响。

SmallChange

1 个答案:

答案 0 :(得分:2)

仅当滚动条为独立时才考虑滚动WPF SmallChange的{​​{1}}属性(这意味着ScrollBar中的 )。由于您在控制模板中使用ScrollViewer,因此将永远无法使用。 ScrollViewer将始终默认使用16个单位的小额更改。

您现在有两个选择:

  • 在包装器类中实现ScrollViewer(例如,从IScrollInfo派生),将其作为内容附加到滚动查看器,将ContentControl放入此自定义包装器类中,设置TabPanelCanContentScroll的{​​{1}}属性。现在,您可以完全控制滚动了。
  • 使用一点技巧:创建一个外部ScrollViewer,并使其滚动true

我向您展示第二种方法:

在您的ScrollBar下,添加一个新的滚动条:

ScrollViewer

您可以根据需要更改ScrollViewer属性。

更新两个滚动命令以该滚动条为目标,而不是<ScrollBar x:Name="myScrollBar" Grid.Row="0" Grid.Column="1" Orientation="Horizontal" Visibility="Collapsed" Tag="{Binding ElementName=sv}" Minimum="0" Maximum="{Binding ScrollableWidth, ElementName=sv}" ViewportSize="{Binding ViewportWidth, ElementName=sv}" Value="{Binding HorizontalOffset, ElementName=sv, Mode=OneWay}" SmallChange="100" Scroll="MyScrollBar_OnScroll"/>

SmallChange

最后,您需要将外部滚动条连接到ScrollViewer

<RepeatButton
    <!-- ... -->
    CommandTarget="{Binding ElementName=myScrollBar}"/>

(您可能要为此制作一个ScrollViewer,以使代码隐藏为空,避免使用void MyScrollBar_OnScroll(object sender, ScrollEventArgs e) { ScrollBar sb = (ScrollBar)sender; (sb.Tag as ScrollViewer)?.ScrollToHorizontalOffset(e.NewValue); } 属性,并允许可重用​​性。)