WPF: How can I KEEP the same ItemTemplate instance once its created ??

Posted by Samir Sabri on Stack Overflow See other posts from Stack Overflow or by Samir Sabri
Published on 2010-05-10T10:47:05Z Indexed on 2010/05/10 10:54 UTC
Read the original article Hit count: 268

Filed under:
|

Hello,

Here is a cinario:

I have a ListView, with ItemsSource = ProjectModel.Instance.PagesModelsCollection; where PagesModelsCollection is an ObservableCollection

In the ListView XAML part:

<ListView.ItemTemplate>

            <DataTemplate x:Name="PagesViewDataTemplate">
                <DataTemplate.Resources>
                    <Style x:Key="PageHostStyle" TargetType="{x:Type p:KPage}">
                    </Style>
                </DataTemplate.Resources>
                <StackPanel x:Name="MarginStack" Margin="50,50,50,50" >
                <p:KPage x:Name="PageHost" >
                </p:KPage>
                </StackPanel>
               </DataTemplate>
</ListView.ItemTemplate>

The problem is the ITemTemplate is re-created each time we refresh the Items. So, if we have 100 Item in the list view, another 100 new ItemTemplate instance will be created if we refresh the items!

As a result, if we add UIElements on one of the ItemTemplate intances, those added UIElements will be lost, because the old ITemTemplate is replaced with a new one!

How can I KEEP the ItemTemplate instance once its created ??

© Stack Overflow or respective owner

Related posts about itemtemplate

Related posts about wpf