GilShalit
GilShalit

Reputation: 6463

In WPF 4.0, switching tabs resets usercontrol UI

I have a border that contains a TabControl in a HeaderedContentControl:

<Border Grid.Column="1" 
    Style="{StaticResource MainBorderStyle}">
    <HeaderedContentControl 
              Content="{Binding Path=Workspaces}"
              ContentTemplate="{StaticResource WorkspacesTemplate}"
              Header="Decision Workspaces"
              Style="{StaticResource MainHCCStyle}"/>
</Border>

The TabControl is defined in a static resource:

<DataTemplate x:Key="ClosableTabItemTemplate">
    <DockPanel Width="120" ToolTip="{Binding Path=DisplayName, Mode=OneTime}">
        <Button 
    Command="{Binding Path=CloseCommand}"
    Content="X"
    Cursor="Hand"
    DockPanel.Dock="Right"
    Focusable="False"
    FontFamily="Courier" 
    FontSize="9"
    FontWeight="Bold"  
    Margin="0,1,0,0"
    Padding="0"
    VerticalContentAlignment="Bottom"
    Width="16" Height="16" 
    />
    <ContentPresenter 
        Content="{Binding Path=DisplayName, Mode=OneTime}"
        VerticalAlignment="Center" 
    />
    </DockPanel>
</DataTemplate>

<!--
This template explains how to render the 'Workspace' content area in the main window.
-->
<DataTemplate x:Key="WorkspacesTemplate">
    <TabControl 
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding}" 
  ItemTemplate="{StaticResource ClosableTabItemTemplate}"
  Margin="4"
  />
</DataTemplate>

The Workspaces property which is bound to the HeaderedContentControl's Content property, has a collection of UserControls, which are rendered in the tabs. This all works fine.

The problem is that when i select a row in a grid in one of the UserControls, switch to a different tab, and then return, the selected row is reset. The same happens if a RowDetails is open - when I switch away and back to the tab, it is collapsed.

Any way around this?

Edit: After looking at the proposed solutions for the TabControl behaviour, I'm wandering if I might ditch it altogether. Any ideas for a UI that will allow me to keep several relatively complex UserControls and switch between them, not loosing the visuals in the process?

Thanks!

Upvotes: 2

Views: 1246

Answers (1)

CodeNaked
CodeNaked

Reputation: 41393

This is a common problem with the TabControl. Since it only displays the content of the selected tab. If your tab items are not visuals themselves and are presented with a DataTemplate, then the controls will be created and released as you switch tabs.

There are two solutions to this problem here and here, which attempt to retain the visuals for each tab.

Upvotes: 4

Related Questions