WPF Flexible TabControl标头

时间:2015-02-13 09:55:30

标签: wpf xaml tabcontrol

我希望TabControl有多个TabItems。这些TabItems每个都有一个标题文本。这些文本的长度可能有很大差异(如5个字符长和15个字符长)。

我希望TabControl仅将标题排成一行。

所有标签标题应使用相同的宽度,当有足够的可用空间时,我希望它们使用所有可用空间,最多为MaxWidth,这对所有项目都是相同的。

因此,如果我想对7个项目使用100的vMaxWidth`,则标签标题的宽度最大为700。如果有更多可用空间,则应忽略它。

如果可用空间较少,我希望在项目之间平均分配该空间。如果文本被截断,我想使用TextWrapping

我现在尝试了多种解决此问题的方法,这是我当前的设置:

<Style x:Key="Style-TabControl-Main" TargetType="{x:Type TabControl}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid KeyboardNavigation.TabNavigation="Local">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Border BorderThickness="0,0,0,1" Margin="13,0,0,0" BorderBrush="{StaticResource Brush-White}">
                        <StackPanel Panel.ZIndex="1" x:Name="HeaderPanel" IsItemsHost="True" KeyboardNavigation.TabIndex="1" Background="Transparent" 
                                    Orientation="Horizontal"/>
                    </Border>

                    <Border x:Name="Border"
                            Grid.Row="1" Grid.ColumnSpan="2"
                            KeyboardNavigation.TabNavigation="Local"
                            KeyboardNavigation.DirectionalNavigation="Contained"
                            KeyboardNavigation.TabIndex="2"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                        <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

TabItem Style

<Style x:Key="Style-TabItem-Main" TargetType="{x:Type TabItem}">
    <Setter Property="Height" Value="31"/>
    <Setter Property="Width" Value="180" />
    <Setter Property="Foreground" Value="{DynamicResource Brush-BrightRegular-Foreground}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Border x:Name="Border" Cursor="Hand" 
                        Margin="2,0,0,0"
                        BorderThickness="1,1,1,0"
                        CornerRadius="4,4,0,0"
                        BorderBrush="{DynamicResource Brush-BrightRegular-Background}"
                        Background="{DynamicResource Brush-White}">
                    <ContentPresenter x:Name="Content" VerticalAlignment="Center" HorizontalAlignment="Stretch" ContentSource="Header" RecognizesAccessKey="True"
                                        TextBlock.TextAlignment="Center" TextBlock.FontSize="16" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected"   Value="True">
                        <Setter  Property="Foreground" Value="{DynamicResource Brush-White}"/>
                        <Setter TargetName="Border"  Property="Background" Value="{DynamicResource Brush-DefaultDark-Background}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我使用StackPanel代替TabPanel来摆脱&#34;堆叠&#34;,当您调整默认TabControl时,会发生这种情况。但是,我无法满足其他要求。我尝试将MaxWidth(而不是固定宽度)应用于TabItem标题,但这当然不起作用,因为项目缩小到所需的最小尺寸。

2 个答案:

答案 0 :(得分:5)

第1步(第一次尝试):将标题放在一行中,并为每个标题指定相同的宽度。

这可以通过使用UniformGrid代替标准TabPanel来实现,并将其行数锁定为1.以下是TabControl样式的精简版本:

<Style x:Key="Style-TabControl-Main" TargetType="{x:Type TabControl}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabControl}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <Border>
                        <UniformGrid x:Name="HeaderPanel" IsItemsHost="True" 
                                     Rows="1" />
                    </Border>

                    <Border x:Name="Border" Grid.Row="1" 
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                        <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

第2步:将标题限制为MaxWidth并应用文字换行。

可以MaxWidth样式设置TabItem以及包装文本的HeaderTemplate(您仍然可以在此处使用自定义ControlTemplate来设置TabItem部分的样式):

<Style x:Key="Style-TabItem-Main" TargetType="{x:Type TabItem}">
    <Setter Property="MaxWidth" Value="100" />
    <!--https://social.msdn.microsoft.com/forums/vstudio/en-US/df4f7fc3-f0ec-4ed1-a022-a32650e49cb3/how-to-wrap-header-text-in-tabcontrol-->
    <Setter Property="HeaderTemplate" >
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}" TextWrapping="Wrap" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        ...
    </Setter>
</Style>


疑难解答现在,如果您在第2步中应用MaxWidth,那么您可能希望在UniformGrid时左对齐<UniformGrid x:Name="HeaderPanel" IsItemsHost="True" Rows="1" HorizontalAlignment="Left" /> TabControl太宽了..

HorizontalAlignment

..但是当MaxWidth尚未到达时你不想要,并且这些项目应该延伸到TabControl的整个宽度(又名 Step 1 ) 。因此,我们需要一种方法来切换public class UniformTabPanel : UniformGrid { public UniformTabPanel() { this.IsItemsHost = true; this.Rows = 1; //Default, so not really needed.. this.HorizontalAlignment = HorizontalAlignment.Stretch; } protected override Size MeasureOverride(Size constraint) { var totalMaxWidth = this.Children.OfType<TabItem>().Sum(tab => tab.MaxWidth); if (!double.IsInfinity(totalMaxWidth)) { this.HorizontalAlignment = (constraint.Width > totalMaxWidth) ? HorizontalAlignment.Left : HorizontalAlignment.Stretch; } return base.MeasureOverride(constraint); } } ,具体取决于项目是否为&#39;已达到MaxWidth(如果已设置)。

第1步(重访):让我们尝试制作自己的UniformGrid:

UniformGrid

现在,我们可以在TabControl这个新面板中替换 ... <Border> <mycontrols:UniformTabPanel x:Name="HeaderPanel" /> </Border> ... 样式:

{{1}}

...并且TabControl应该按照已经过时的方式运行。

答案 1 :(得分:0)

Sphinxx的答案是正确的,但是我需要将以下代码添加到UniformTabPanel,以使其像我想要的那样工作(当有足够的空间可用时将标题大小调整为maxwidth)

我将以下代码添加到UniformTabPanel,它现在可以满足我的需求:

protected override Size MeasureOverride(Size constraint)
{
    var children = this.Children.OfType<TabItem>();
    var totalMaxWidth = children.Sum(tab => tab.MaxWidth);
    if (!double.IsInfinity(totalMaxWidth))
    {
        this.HorizontalAlignment = (constraint.Width > totalMaxWidth)
                                            ? HorizontalAlignment.Left
                                            : HorizontalAlignment.Stretch;
        foreach (var child in children)
        {
            child.Width = this.HorizontalAlignment == System.Windows.HorizontalAlignment.Left
                    ? child.MaxWidth
                    : Double.NaN;
        }
    }
    return base.MeasureOverride(constraint);
}