我有一个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
答案 0 :(得分:2)
仅当滚动条为独立时才考虑滚动WPF SmallChange
的{{1}}属性(这意味着ScrollBar
中的不 )。由于您在控制模板中使用ScrollViewer
,因此将永远无法使用。 ScrollViewer
将始终默认使用16个单位的小额更改。
您现在有两个选择:
ScrollViewer
(例如,从IScrollInfo
派生),将其作为内容附加到滚动查看器,将ContentControl
放入此自定义包装器类中,设置TabPanel
至CanContentScroll
的{{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);
}
属性,并允许可重用性。)