TabControl与单行TabPanel和溢出面板

时间:2016-02-05 08:52:47

标签: wpf tabcontrol

我想改变WPF TabControl的功能,只生成一行,并为每个其他项创建一个Overflow Popup(如ToolBar / ToolBarOverflowPanel)。目前VisualStudio中显示标签的方式相同。

这就是我得到的:

<Style TargetType="TabControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabControl">
                <Grid KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <controls:OneLineTabPanel x:Name="HeaderPanel"
                                              Grid.Row="0"
                                              Panel.ZIndex="1"
                                              Margin="0"
                                              KeyboardNavigation.TabIndex="1"
                                              Background="Transparent">
                    </controls:OneLineTabPanel>
                 ......

我现在尝试改变TabPanel(用于显示标题),但我无法更改其模板(因为它来自TabPanel)。所以我尝试从其他控件中派生出来,但后来我根本看不到任何项目。

如何让自己的ItemsControl与TabControl配合使用?

2 个答案:

答案 0 :(得分:5)

您需要覆盖TabControl的整个模板。
幸运的是,使用Visual Studio轻松提取模板副本(右键单击TabControl元素上的文档大纲窗口 - &gt;编辑模板 - >编辑副本)。

在默认的TabControl中模板HeaderPanel以下列方式定义:

<TabPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>

主要思想是通过设置IsItemsHost =“true”将TabControl.Items绑定到Panel控件,而不是完全另一种控件类型。 例如,要绑定到ToolBar,它具有开箱即用的溢出按钮功能:

<ToolBar x:Name="HeaderPanel" ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Items}"/>

以下是使用ToolBar作为标题面板的TabControl模板的完整示例:

<TabControl>
    <TabControl.Template>
        <ControlTemplate TargetType="{x:Type TabControl}">
            <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition x:Name="ColumnDefinition0"/>
                    <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                    <RowDefinition x:Name="RowDefinition1" Height="*"/>
                </Grid.RowDefinitions>

                <!--this will do the trick!!!-->
                <ToolBar x:Name="HeaderPanel" ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Items}"/>

                <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                    <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="TabStripPlacement" Value="Bottom">
                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/>
                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                    <Setter Property="Margin" TargetName="HeaderPanel" Value="2,0,2,2"/>
                </Trigger>
                <Trigger Property="TabStripPlacement" Value="Left">
                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                    <Setter Property="Margin" TargetName="HeaderPanel" Value="2,2,0,2"/>
                </Trigger>
                <Trigger Property="TabStripPlacement" Value="Right">
                    <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                    <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/>
                    <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/>
                    <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                    <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                    <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                    <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                    <Setter Property="Margin" TargetName="HeaderPanel" Value="0,2,2,2"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </TabControl.Template>
</TabControl>

答案 1 :(得分:2)