Pergunta

I'm having various problems with the virtualisation of views in the TabControl. Luckily, I've found something that I think will solve all my problems in this CodeProject article.

One problem is introduced by this solution though, and that is that it destroys my HeaderTemplate. The tab header has the same content as the content control.

My view uses a TabControl like this:

<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>

The behaviors:TabItemGeneratorBehavior is from the CodeProject article. Inside the TabItemGeneratorBehavior is a method that generates TabItems:

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);
}

I think my problem is with the line that sets the binding for the HeaderProperty. How can I set the binding so that it uses the HeaderTemplate defined in my XAML above?

Foi útil?

Solução

OP here.

The solution is to remove the HeaderTemplate assignment when creating the TabItem:

var tab = new TabItem
    {
        DataContext = item,
        Content = contentControl,
        // HeaderTemplate = _tabControl.ItemTemplate
    };
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top