Wpf TabItem headers:Content不';如果实际的选项卡标题没有拉伸,则不会拉伸

Wpf TabItem headers:Content不';如果实际的选项卡标题没有拉伸,则不会拉伸,wpf,Wpf,我有一个TabControl,并试图让标题看起来很好。我有以下XAML: (在Resources.xaml:) 以下是我的问题的一个可视示例: 我现在喜欢标签的实际宽度,但是我的标题dockpanel没有填满所有的空间,这一事实让人感到不安。如果我也使用网格(可能是任何容器控件),也会发生同样的情况。尝试覆盖ItemContainerStyle.Template而不是ContentTemplate 我在过去做过类似的事情,虽然我重写了整个TabControl模板,所以很难看到我在做什么。我的代

我有一个TabControl,并试图让标题看起来很好。我有以下XAML:

(在
Resources.xaml
:)

以下是我的问题的一个可视示例:


我现在喜欢标签的实际宽度,但是我的标题dockpanel没有填满所有的空间,这一事实让人感到不安。如果我也使用
网格
(可能是任何容器控件),也会发生同样的情况。

尝试覆盖
ItemContainerStyle.Template
而不是
ContentTemplate

我在过去做过类似的事情,虽然我重写了整个
TabControl
模板,所以很难看到我在做什么。我的代码如下所示:

<ControlTemplate x:Key="CurvedTabItemTemplate" TargetType="{x:Type TabItem}">
    <DockPanel Width="200">
        <ContentPresenter x:Name="ContentSite" ContentSource="Header" RecognizesAccessKey="True" />
    </DockPanel>
</ControlTemplate>

<Style x:Key="CurvedTabItemStyle" TargetType="{x:Type TabItem}">
    <Setter Property="Template" Value="{StaticResource CurvedTabItemTemplate}" />
</Style>

<TabControl Grid.Column="1" ItemsSource="{Binding Path=Tabs}" 
            ContentTemplate="{DynamicResource ClosableTabItemTemplate}"
            ItemContainerStyle="{DynamicResource CurvedTabItemStyle}" />

我遇到了相同的可关闭选项卡的视觉问题,并通过扩展
选项卡控件(稍后在XAML中使用)和
选项卡项
类解决了这个问题。我在前者中重写了
GetContainerForItemOverride()
,在后者中重写了
OnApplyTemplate()
。我创建了一个
DependencyObject
扩展方法来查找
TabItem
ContentPresenter
元素,并将其
HorizontalAlignment
属性设置为
HorizontalAlignment.Stretch

public class MyTabControl : TabControl
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new MyTabItem();
    }
}

public class MyTabItem : TabItem
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var content = this.GetFirstDescendantOfType<ContentPresenter>();
        if (content != null)
        {
            content.HorizontalAlignment = HorizontalAlignment.Stretch;
        }
    }
}

public static class DependencyObjectExtensions
{
    public static T GetFirstDescendantOfType<T>(this DependencyObject parent) where T : DependencyObject
    {
        if (parent == null)
        {
            return null;
        }
        else
        {
            var children = parent.GetChildren();
            T descendant = children.FirstOrDefault(child => child is T) as T;

            if (descendant == null)
            {
                foreach (var child in children)
                {
                    if ((descendant = child.GetFirstDescendantOfType<T>()) != null) break;
                }
            }

            return descendant;
        }
    }

    public static IEnumerable<DependencyObject> GetChildren(this DependencyObject parent)
    {
        var children = new List<DependencyObject>();

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            children.Add(VisualTreeHelper.GetChild(parent, i));
        }

        return children;
    }
}
(在
main window.xaml
):注意自定义
MyTabControl
控件的使用

<MyTabControl IsSynchronizedWithCurrentItem="True"
              ItemsSource="{Binding Path=ClosableViewModels}"
              ItemTemplate="{StaticResource ClosableTabItemTemplate}"
              Margin="4" />


ContentPresenter.HorizontalAlignment
TabItem
的默认样式中绑定到
ItemsControl.HorizontalContentAlignment
。这是从Aero.NormalColor.xaml获取的:

<ContentPresenter Name="Content"
                  ContentSource="Header"
                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                  HorizontalAlignment="{Binding Path=HorizontalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                  VerticalAlignment="{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                  RecognizesAccessKey="True"/>

这给了我一些奇怪的行为,标签的内部内容和标签标题被翻转。。。我会继续尝试不同的东西,明白了。原来我看的文件完全错了。我爱你!我一直在寻找2个小时,试图让我的自定义TabHeader UserControl填充/拉伸选项卡的宽度。您的ControlHelper和OnApplyTemplate()解决了我的问题。太好了!这比其他解决方案简单得多。一行很好,很明显我应该看到它。很好而且简单的答案!:)
<DataTemplate x:Key="ClosableTabItemTemplate">
    <DockPanel>
        <Button Command="{Binding Path=CloseCommand}"
                Content="X"
                Cursor="Hand"
                DockPanel.Dock="Right"
                Focusable="False"
                FontFamily="Courier"
                FontSize="9"
                FontWeight="Bold"
                Margin="8,1,0,0"
                Padding="0"
                VerticalContentAlignment="Bottom"
                Width="16"
                Height="16"
                ToolTip="Close" />
        <TextBlock Text="{Binding Path=DisplayName}"
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center" />
    </DockPanel>
</DataTemplate>
<MyTabControl IsSynchronizedWithCurrentItem="True"
              ItemsSource="{Binding Path=ClosableViewModels}"
              ItemTemplate="{StaticResource ClosableTabItemTemplate}"
              Margin="4" />
<ContentPresenter Name="Content"
                  ContentSource="Header"
                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                  HorizontalAlignment="{Binding Path=HorizontalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                  VerticalAlignment="{Binding Path=VerticalContentAlignment,RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                  RecognizesAccessKey="True"/>
<TabControl HorizontalContentAlignment="Stretch" />