在WPF中使选项卡标题根据文本内容自适应大小

3

我对WPF的相关知识不太了解,但是我尝试着自己重新设计一个TabItem。

enter image description here

如您所见,选项卡填满了整个窗口的宽度。与我的原始目的不同,我实际上想要让选项卡的宽度基于其内部文本。就像原始样式一样,只是重新设计过。

我的代码中的样式:

<Style x:Key="ZoidTab" TargetType="{x:Type TabItem}" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate x:Name="ZoidTemplate" TargetType="{x:Type TabItem}">
                <Border Width="Auto" Height="Auto">
                <Grid x:Name="grid">
                    <Polygon
                        Fill="Turquoise"
                        Points="0,1 0.05,0 0.95,0 1,1"
                        Stretch="Fill"
                        Margin="0,0,0,0"
                    />
                    <ContentPresenter x:Name="tabContent" HorizontalAlignment="Center" ContentSource="Header" VerticalAlignment="Center" TextElement.Foreground="#FFFFFFFF"/>
                </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="FontSize" Value="12pt"/>
</Style>

我想知道我必须修复什么才能使宽度正确...谢谢。

1个回答

2
问题在于你的Grid没有ColumnDefinitions部分来限制唯一列的大小。将其修改为以下内容:
<Grid x:Name="grid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接