If you put controls in your DataTemplate, why are their individual states copied or reflected in each and every Tab in the TabControl? You change it in one Tab, all other Tabs reflect, why is that?! It seems to me the TabControl initializes one templated ContentControl only and each click on a Tab copies the entire content in it anew - leaving old controlstates untouched. To see what i mean consider putting this in your XAML-Pad:
<TabControl>
<TabControl.ContentTemplate>
<DataTemplate>
<Border>
<TextBox Text="test"/>
</Border>
</DataTemplate>
</TabControl.ContentTemplate>
<TabItem Header="Tab1"/>
<TabItem Header="Tab2"/>
</TabControl>
It will create a TabControl with two templated tabs. Now enter something in the TextBox and switch to another Tab, the entered text will carry over. Each Tab will have the same content now. I do not observe the same behavior in a ListBox or any other control and it makes practical work very hard because every little bit needs to be bound to a ViewModel to make it usable in a TabControl. I noticed this weird behavior when Expanders i used in a DataTemplate popped open in all of my tabs, although i specifically addressed one. As a workaround i had to bind "IsExpanded" to a property in the ViewModel, but it really sucks having to do that.
Anyone knows what's happening here?
SOLUTION
<TabControl x:Name="MainTab" SelectedIndex="0"/>
...
Collection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Collection_CollectionChanged);
...
void Collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
TabItem MyNewItem = new TabItem();
ContentPresenter MyContentPresenter = new ContentPresenter();
MyContentPresenter.ContentTemplate = (DataTemplate)this.FindResource("MyTemplate");
MyContentPresenter.Content = e.NewItems[0];
MyNewItem.Content = MyContentPresenter;
MainTab.Items.Add(MyNewItem );
}
}