Wpf 以编程方式指定HeaderTemplate的绑定

Wpf 以编程方式指定HeaderTemplate的绑定,wpf,binding,datatemplate,tabcontrol,attachedbehaviors,Wpf,Binding,Datatemplate,Tabcontrol,Attachedbehaviors,我在TabControl中虚拟化视图时遇到了各种问题。幸运的是,我发现了一些我认为可以解决我所有问题的东西 不过,这个解决方案引入了一个问题,那就是它会破坏我的HeaderTemplate。选项卡标题的内容与内容控件的内容相同 “我的视图”使用如下选项卡控件: <TabControl behaviors:TabItemGeneratorBehavior.ItemsSource="{Binding MyItems, Mode=OneWay}" behavior

我在TabControl中虚拟化视图时遇到了各种问题。幸运的是,我发现了一些我认为可以解决我所有问题的东西

不过,这个解决方案引入了一个问题,那就是它会破坏我的HeaderTemplate。选项卡标题的内容与内容控件的内容相同

“我的视图”使用如下选项卡控件:

<TabControl
        behaviors:TabItemGeneratorBehavior.ItemsSource="{Binding MyItems, Mode=OneWay}"
        behaviors:TabItemGeneratorBehavior.SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">

    <TabControl.Resources>
        <Style TargetType="TabItem">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <ContentPresenter>
                            <ContentPresenter.Content>
                                <TextBlock Text="{Binding Title}"/>
                            </ContentPresenter.Content>
                        </ContentPresenter>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TabControl.Resources>
</TabControl>
我认为我的问题在于为HeaderProperty设置绑定的行。如何设置绑定,使其使用上述XAML中定义的HeaderTemplate?

OP here

解决方案是在创建选项卡项时删除HeaderTemplate分配:

private void AddTabItem(object item)
{
    var contentControl = new ContentControl();
    var tab = new TabItem
        {
            DataContext = item,
            Content = contentControl,
            HeaderTemplate = _tabControl.ItemTemplate
        };

    contentControl.SetBinding(ContentControl.ContentProperty, new Binding());
    tab.SetBinding(HeaderedContentControl.HeaderProperty, new Binding());

    _tabControl.Items.Add(tab);
}
var tab = new TabItem
    {
        DataContext = item,
        Content = contentControl,
        // HeaderTemplate = _tabControl.ItemTemplate
    };