We're using a Tabcontrol to display a number of items with rather expensive content, and the issue we're having is that as you iterate over the tabs (selecting them one by one), the responsiveness of the application becomes slower and slower.
This behavior is unexpected as from what I understand, as the selected tab changes, the previously selected tabs content is unloaded first, so that you're only paying the price for one tabs content at a time.
I've managed to simulate the behaviour with the code below. To reproduce :
- Run the application
- Launch the selected tabs contextmenu (the tabs header contextmenu), it will be responsive
- From left to right, go through each tab, selecting one by one
By the time you reach tab ~10, the responsiveness of its contextmenu is now very laggy, as you click a checkbox, its animation takes a few seconds to run through
<Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" WindowStartupLocation="CenterScreen"> <Window.Resources> <Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <CheckBox Content="CheckBox" /> <CheckBox Content="CheckBox" /> <CheckBox Content="CheckBox" /> <CheckBox Content="CheckBox" /> <CheckBox Content="CheckBox" /> </ContextMenu> </Setter.Value> </Setter> </Style> </Window.Resources> <TabControl Name="tabControl" />
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); for (int i = 0; i < 25; i++) { CreateTab(); } } void CreateTab() { var itemsControl = new ItemsControl(); for (int i = 0; i < 1000; ++i) { itemsControl.Items.Add(new TextBox()); } tabControl.Items.Add(new TabItem() { Header = string.Format("Tab{0}", tabControl.Items.Count), Content = itemsControl }); } }