We use the standard WPF TabControl, and the problem is that every time you change SelectedItem, VisualTree goes through it.
As a result, we created a special TabControl (I called it TabControlEx), which saves all the displayed elements, but prefers to just show / hide ContentPresenters for TabItems.
Here is the relevant code
using System; using System.Collections.Specialized; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; namespace MVVM.Demo {
If you want to create a template of this type (you may need to expand it for Left / Right TabStripLocation)
<ControlTemplate x:Key="MainTabControlTemplateEx" TargetType="{x:Type controls:TabControlEx}"> <Grid> <Grid.RowDefinitions> <RowDefinition x:Name="row0" Height="Auto"/> <RowDefinition x:Name="row1" Height="4"/> <RowDefinition x:Name="row2" Height="*"/> </Grid.RowDefinitions> <TabPanel x:Name="tabpanel" Background="{StaticResource OutlookButtonHighlight}" Margin="0" Grid.Row="0" IsItemsHost="True" /> <Grid x:Name="divider" Grid.Row="1" Background="Black" HorizontalAlignment="Stretch"/> <Grid x:Name="PART_ItemsHolder" Grid.Row="2"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="TabStripPlacement" Value="Top"> <Setter TargetName="tabpanel" Property="Grid.Row" Value="0"/> <Setter TargetName="divider" Property="Grid.Row" Value="1"/> <Setter TargetName="PART_ItemsHolder" Property="Grid.Row" Value="2" /> <Setter TargetName="row0" Property="Height" Value="Auto" /> <Setter TargetName="row1" Property="Height" Value="4" /> <Setter TargetName="row2" Property="Height" Value="*" /> </Trigger> <Trigger Property="TabStripPlacement" Value="Bottom"> <Setter TargetName="tabpanel" Property="Grid.Row" Value="2" /> <Setter TargetName="divider" Property="Grid.Row" Value="1" /> <Setter TargetName="PART_ItemsHolder" Property="Grid.Row" Value="0" /> <Setter TargetName="row0" Property="Height" Value="*" /> <Setter TargetName="row1" Property="Height" Value="4" /> <Setter TargetName="row2" Property="Height" Value="Auto" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
What could you use like this
<local:TabControlEx IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Workspaces}" Template="{StaticResource MainTabControlTemplateEx}"> </local:TabControlEx>
It works very well, and we use it with great effect for a long time.
sacha
source share