wpf - 想要为tabcontrol制作可滚动的选项卡

标签 wpf silverlight

假设我有一个标签控件,并且我有超过50个标签,而这些地方没有足够的空间容纳这么多个标签,那么如何使这些标签可滚动?

最佳答案

覆盖TabControl ControlTemplate并在ScrollViewer周围添加一个TabPanel,例如以下示例:

<Grid>
    <TabControl>
        <TabControl.Template>
            <ControlTemplate TargetType="TabControl">
                <StackPanel>
                    <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
                        <TabPanel x:Name="HeaderPanel"
                              Panel.ZIndex ="1" 
                              KeyboardNavigation.TabIndex="1"
                              Grid.Column="0"
                              Grid.Row="0"
                              Margin="2,2,2,0"
                              IsItemsHost="true"/>
                    </ScrollViewer>
                    <ContentPresenter x:Name="PART_SelectedContentHost"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          Margin="{TemplateBinding Padding}"
                                          ContentSource="SelectedContent"/>
                </StackPanel>
            </ControlTemplate>
        </TabControl.Template>
        <TabItem Header="TabItem1">TabItem1 Content</TabItem>
        <TabItem Header="TabItem2">TabItem2 Content</TabItem>
        <TabItem Header="TabItem3">TabItem3 Content</TabItem>
        <TabItem Header="TabItem4">TabItem4 Content</TabItem>
        <TabItem Header="TabItem5">TabItem5 Content</TabItem>
        <TabItem Header="TabItem6">TabItem6 Content</TabItem>
        <TabItem Header="TabItem7">TabItem7 Content</TabItem>
        <TabItem Header="TabItem8">TabItem8 Content</TabItem>
        <TabItem Header="TabItem9">TabItem9 Content</TabItem>
        <TabItem Header="TabItem10">TabItem10 Content</TabItem>
    </TabControl>
</Grid>

得到以下结果:

关于wpf - 想要为tabcontrol制作可滚动的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5690594/

相关文章:

c# - 在依赖对象的 XAML 中启用直接内容创建

silverlight - 为什么使用 Dispatcher.BeginInvoke?

.net - 错误: Cannot find the Style Property 'Template' on the type 'System.Windows.Controls.Primitives.Popup'

c# - WPF 数据绑定(bind)魔法

WPF:在没有自定义镶边的无边框窗口上放置阴影

c# - typeof(object).TypeHandle.Value 的替代方案

c# - 如何在 WPF 中的窗口之间导航?

c# - 如何防止 ListView 扩展窗口维度?

c# - 长类名会影响 XAP 文件的大小吗?

.net - 适用于大量数据的最佳抽取算法有哪些?