This is a migrated thread and some comments may be shown as answers.

MVVM Style ContentTemplateSelector

2 Answers 126 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Maxim
Top achievements
Rank 1
Maxim asked on 18 Mar 2011, 07:03 PM
Hi!

I'm playing with TabControlHeadTemplate example from this forum.
But instead of oversimplified:
            <DataTemplate x:Key="ContentTemplateAlpha">
                <Rectangle Fill="Aqua" Width="200" Height="200" /> 
            </DataTemplate>
 
            <DataTemplate x:Key="ContentTemplateBetta">
                <Rectangle Fill="Red" Width="200" Height="200" />
            </DataTemplate>

I re-factored it to more realistic:

            <DataTemplate x:Key="ContentTemplateAlpha">
                <local:UserControlAqua/>
            </DataTemplate>
 
            <DataTemplate x:Key="ContentTemplateBetta">
                <local:UserControlRed/>
            </DataTemplate>

Now, every time I switch tabs, TabControl recreates my usercontrols (hits constructor's breakpoint).
Is that how it supposed to work? Is there any way to re-use in-memory views?

My concern is that for complex usercontrols it can take a long time to re-bind UI to a ViewModel.

Thank you,
Maxim

2 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 18 Mar 2011, 08:08 PM
Hello Maxim,

There is a property called IsContentPreserved that determines the way TabControl works. If IsContentPreserved=false (default) ther will be single ContentPresenter used by the TabControl to visualize its children. This means the content is recreated every time the selection is changed.
When this option is set to True the TabControl dynamically creates separate ContentPresenter for every selected item. Thus reselecting the item will not force content recreation but will increase the memory usage.

Regards,
Hristo
the Telerik team
0
Maxim
Top achievements
Rank 1
answered on 18 Mar 2011, 09:33 PM
Hi Hristo,

Thank you very much for your answer. That's exactly what I was hoping to find!

Maxim

Tags
TabControl
Asked by
Maxim
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Maxim
Top achievements
Rank 1
Share this question
or