WPF: How can I KEEP the same ItemTemplate instance once its created ??
- by Samir Sabri
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 ??