【发布时间】:2015-04-14 07:55:34
【问题描述】:
我想要一个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>
还有TabItemStyle
<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 来消除在调整默认TabControl 大小时发生的“堆叠”。但是,我无法满足我的其余要求。我尝试将MaxWidth(而不是固定宽度)应用于TabItem 标头,但这当然不起作用,因为该项目会缩小到所需的最小尺寸。
【问题讨论】:
-
我之前在 TabControl 上工作过,所以我不能保证这会起作用,但首先你不需要
<Style x:Key="Style-TabItem-Main" TargetType="{x:Type TabItem}">一个简单的<Style TargetType="{x:Type TabItem}">就可以了,其次删除@987654338 @ 和HorizontalAlignment来自您的ContentControl,我注意到这会弄乱拉伸内容的逻辑。顺便说一句,尝试将 ContentPresenter 包装在 DockPanel 中。 HTH -
我实际上需要键入,因为我使用了多种不同的 TabControl 和 TabItem 样式。这只是其中之一!
标签: wpf xaml tabcontrol