我有一个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>
当我按下重复按钮时,滚动的量很小,只有 one 宽度的一小部分TabItem
。
我希望这个更大。我想(最小)滚动TabItem
.
我已经使用了CanContentScroll
a的属性ScrollViewer
,但这不会改变任何东西。
我还尝试更改SmallChange
水平滚动条上的属性,但这也没有影响。
myTab.ApplyTemplate();
var scrollviewer = myTab.Template.FindName("sv", myTab) as ScrollViewer;
if (scrollviewer != null)
{
scrollviewer.ApplyTemplate();
_scrollBar = scrollviewer.Template.FindName("PART_HorizontalScrollBar", scrollviewer) as ScrollBar;
if (_scrollBar != null)
{
_scrollBar.SmallChange = 0.5;
}
}
SmallChange
WPF的属性ScrollBar
仅在滚动条是独立的(即不在中ScrollViewer
)时才考虑用于滚动。由于您ScrollViewer
在控制模板中使用了 a ,因此这将永远不会起作用。AScrollViewer
将始终应用 16 个单位的默认小更改。
你现在有两个选择:
IScrollInfo
在包装类中实现(例如从 a 派生ContentControl
),将其作为内容附加到滚动查看器,放在TabPanel
这个自定义包装类的内部,将 的CanContentScroll
属性设置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);
}
(您可能希望利用Behavior
这一点来保持代码隐藏为空,避免使用该Tag
属性,并允许可重用性)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句