2

I have a problem with the WPF tab control.

I have a TabControl, with the ItemsSource bound to an ObservableCollection. I created a data template for the header/content portion of the tabs. The content portion contains a custom control, with a bunch of labels and text boxes. For the text boxes that are editable when a new tab is created that data carries over and appears in the new tab. Not sure if it's a problem with my XAML or something in the view model. Here's my code for the XAML:

<UserControl.Resources>
  <DataTemplate x:Key="TabItemHeaderTemplate">
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding AdFile.Name}" />
    </StackPanel>
  </DataTemplate>
  <DataTemplate x:Key="TabItemContentTemplate">
    <MyView:MyCustomControl/>
  </DataTemplate>
  <Style x:Key="TabItemContainerStyle" TargetType="TabItem">
    <Setter Property="Header" Value="{Binding}"/>
    <Setter Property="HeaderTemplate" 
    Value="{StaticResource TabItemHeaderTemplate}"/>
    <Setter Property="Content" Value="{Binding}"/>
    <Setter Property="ContentTemplate" 
    Value="{StaticResource TabItemContentTemplate}"/>
  </Style>
</UserControl.Resources>


<TabControl Grid.Row="3" ItemsSource="{Binding OpenedFiles}" x:Name="_myTabControl" SelectedItem="{Binding Path=CurrentDataControlViewModel, Mode=TwoWay}" SelectionChanged="TabControlSelectionChanged" ItemContainerStyle="{StaticResource TabItemContainerStyle}"/>

Not sure what other information I should provide. Maybe this is a common problem and I am just not setting something up correctly? Basically I just want to be able to create a new instance of the control for every tab...

Thanks in advance.

Fred
  • 3,365
  • 4
  • 36
  • 57
  • Show how do you create new tabs. Also, please be more specific about what data is carried over from where. – Anton Tykhyy Mar 12 '13 at 11:34
  • Can you describe in more detail, what behavior you want to achieve and what behavior you see at the moment? – Marc Mar 12 '13 at 11:37
  • That's normal TabControl behavior. You are setting the `ContentTemplate` to a UserControl, and since the template doesn't change when you switch tabs, WPF sees no reason to re-draw a new instance of the template. I usually use a customized version of the TabControl which may fix your problem. The code can be found [here](http://stackoverflow.com/a/8619120/302677) – Rachel Mar 12 '13 at 12:48
  • Is there a way to force the refresh? Seems silly I'd need to do all this work, and I can't quite understand why that's default behavior. In my case each tab is essentially a form, and all fields are data bound. But when I create a new tab, obviously I don't want the fields populated with the previous tabs data. – user1646235 Mar 12 '13 at 13:31

1 Answers1

0

It sounds as though your ViewModel is a Singleton, being cloned, or you're trying to populate the new tab with the existing ViewModel.

If you're using MEF, remember to set the [PartCreationPolicy] attribute to NonShared.

Barracoder
  • 3,696
  • 2
  • 28
  • 31