MVVM Style ContentTemplateSelector

3 posts, 0 answers
  1. Maxim
    Maxim avatar
    2 posts
    Member since:
    Mar 2011

    Posted 18 Mar 2011 Link to this post

    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. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 18 Mar 2011 Link to this post

    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
  3. DevCraft banner
  4. Maxim
    Maxim avatar
    2 posts
    Member since:
    Mar 2011

    Posted 18 Mar 2011 Link to this post

    Hi Hristo,

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

    Maxim

Back to Top