tags:

views:

14

answers:

0

I want to create a TabControl with a number of "static" TabItems (explicitly typed in XAML) and a number of dynamically added TabItems. To achieve this I tried to use a CompositeCollection as the TabControl.ItemSource.

Sample code:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        >
  <Window.Resources>
    <x:Array x:Key="SomeTexts" x:Type="sys:String">
      <sys:String>Text1</sys:String>
      <sys:String>Text2</sys:String>
    </x:Array>
  </Window.Resources>

  <TabControl>
    <TabControl.ItemsSource>
      <CompositeCollection>
        <TabItem Header="Test">
          <StackPanel>
            <TextBlock x:Name="MyText" Text="Blah" />
            <TextBlock Text="{Binding Text, ElementName=MyText}" />
          </StackPanel>
        </TabItem>
        <CollectionContainer Collection="{StaticResource SomeTexts}" />
      </CompositeCollection>
    </TabControl.ItemsSource>
  </TabControl>
</Window>

This example has one fixed tab item and three "dynamic" tab items (note that 'SomeTexts' is a fixed array here just to ease the example; in the real code it will be a dynamic collection).

The example works except for the 'ElementName' binding, which does not work. I suppose this is because the CompositeCollection is not a Freezable (see also Why is CompositeCollection not Freezable?).

Does anyone has a solution?