WPF tabcontrol tabitem标题

时间:2012-09-14 13:21:56

标签: wpf tabcontrol

我已经在tabcontrol中设置了这个tabitem

<Style x:Key="TabHeaderStyle" TargetType="{x:Type TabItem}">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate >
                    <Grid HorizontalAlignment="Stretch" Height="22">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="20" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2"  ></RowDefinition>
                            <RowDefinition Height="Auto" ></RowDefinition>
                        </Grid.RowDefinitions>
                        <Rectangle Fill="{Binding  RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Content.DataContext.HeaderColor}" Grid.ColumnSpan="2"></Rectangle>
                        <TextBlock Grid.Row="1" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Content.DataContext.HeaderInfo}"
                                       VerticalAlignment="Bottom" Margin="4,0,8,0"/>
                        <Button Grid.Row="1" Grid.Column="1" Content="x" ToolTipService.ToolTip="Close this view." BorderBrush="{x:Null}" Background="{x:Null}" Foreground="#FF224A71" VerticalAlignment="Center" Padding="3,0">
                            <Button.OpacityMask>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="Black" Offset="0"/>
                                    <GradientStop Color="#4BFFFFFF" Offset="1"/>
                                </LinearGradientBrush>
                            </Button.OpacityMask>
                            <ei:Interaction.Triggers>
                                <ei:EventTrigger EventName="Click">
                                    <Controls:CloseTabbedViewAction />
                                </ei:EventTrigger>
                            </ei:Interaction.Triggers>
                        </Button>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

当我有几个标签打开时,这个标题看起来不错

enter image description here

这看起来很好用“x”,右侧对齐的近视图。我的问题是,当我打开更多选项卡时,标准tabcontrol功能是在选项卡的空间很小时添加一行。当发生这种情况时,tabitems会被调整大小,但似乎它们不会通知childcontrols,因为标签看起来像这样

enter image description here

请注意,“x”未像以前那样右对齐。我试图将datatemplate基于网格和stackpanel以不同的风格为基础无济于事。任何人都知道即使我有多行标签,我将如何使“x”按钮右对齐。

2 个答案:

答案 0 :(得分:1)

您的Grid ColumnDefinitions将限制其宽度(Auto表示自动调整内容大小

而是尝试:

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

(另外,我不知道是否会有所作为,但在我的TabItem款式中,我通常会将Template设为ControlTemplate,而不是设置{{1}正如你所做的那样HeaderTemplate

答案 1 :(得分:0)

在TabItem模板中尝试此操作...

 <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
 <Setter Property="VerticalContentAlignment" Value="Stretch"/>

如果它解决了您的问题,请告诉我。我不能在这里测试你的代码。