How can I change the DataTemplate for the SelectedItem of ListView in UWP project? I\'m trying to use the
One way is to use RegisterPropertyChangedCallback
to monitor the changes of the IsSelected
property and then update the ContentTemplateSelector
of the ListViewItem
manually -
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (container is ListViewItem cont)
{
if (cont.Tag != null && long.TryParse(cont.Tag.ToString(), out var token))
{
cont.UnregisterPropertyChangedCallback(ListViewItem.IsSelectedProperty, token);
}
cont.Tag = cont.RegisterPropertyChangedCallback(ListViewItem.IsSelectedProperty, (s, e) =>
{
cont.ContentTemplateSelector = null;
cont.ContentTemplateSelector = this;
});
if (cont.IsSelected)
{
return SelectedItemTemplate;
}
return DefaultTemplate;
}
return DefaultTemplate;
}