0

This is a question of curiosity, I have no practical use for such expensive layouts.

I have the following Xaml

<Window x:Class="WpfApplication16.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen">
    <TabControl ItemsSource="{Binding Items}">
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ScrollViewer>
                    <ContentPresenter Content="{Binding View}" />
                </ScrollViewer>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
</Window>

and the following code behind

public class Item
    {
        public FrameworkElement View { get { return m_view; } }

        public Item()
        {
            for (int i = 0; i < 3000; ++i)
            {
                m_view.Items.Add(new TextBox());
            }
        }

        ItemsControl m_view = new ItemsControl();
    }

    public partial class MainWindow : Window
    {
        public List<Item> Items { get; private set; }

        public MainWindow()
        {
            Items = new List<Item>();
            for (int i = 0; i < 10; ++i)
            {
                Items.Add(new Item());
            }

            DataContext = this;
            InitializeComponent();
        }
    }

So before the window is initialized all the TextBoxes have already been constructed, all 30,000 of them, but they haven't been loaded yet. They only get loaded/unloaded when the selection is changed, such as when the initial selection takes place. I would expect changing tab the first time for each item to take a while due to the sheer amount of textboxes, but why is it that after Ive selected each tab, going through them again still takes a long time between load/unload?

There is no binding going on, and I don't think its a layout issue as when I resize the window it updates a lot faster than it does between changing tab. I don't think its a memory issue either (stuff having to be loaded in and out) as the memory usage grows between selecting tabs and then stays constant after all 10 have been selected, and run through again.

Also I'm purposely not using visualization.

pastillman
  • 1,104
  • 2
  • 16
  • 27

1 Answers1

1

That's the default behaviour of TabControl and there is no easy ways of changing it.

It unloads the visual tree of a tab as soon as it becomes inactive to save memory and then loads that back when the tab becomes active again.

If you ever do encounter this problem, you have two options:

  1. There was a blog post on plural sight, but they changed their website and it's no longer available. They basically subclassed TabControl and changed this behaviour so it caches the tab contents. I think the code is saved here (or a derivative of it): WPF TabControl - Preventing Unload on Tab Change?

  2. Another option (and better in my opinion) is to buy\download a third party control that already does that. I believe DevExpress TabControl does cache its tabs and doesn't have to redraw them when you switch between tabs. http://documentation.devexpress.com/#wpf/CustomDocument7975

Community
  • 1
  • 1
Fayilt
  • 1,042
  • 7
  • 16