WPF – CollectionView Refresh in TreeView without losing selection

Why is this so freaking hard to do!?

I have the following:

public BindingList<Dialogue> Dialogues { get; set; } public CollectionView DialoguesCollectionView { get; set; } 

DialoguesCollectionView is a ListCollectionView injecting Dialogues with a few SortDescriptions.

Whenever something in Dialogues changes I have to Refresh the CollectionView, whereas you would expect it happens automagically whenever Dialogues triggers a OnPropertyChanged (which I do from its ListChanged event).

Even worse so, upon Refresh, a TreeView loses the currently selected item and instead jumps back to the parent of the TreeViewItem, whereas I want it to re-select the item (unless it gets removed, obviously).

My TreeView is a bunch of nested HierarchicalDataTemplate, complicating this even further.

Could anyone assist me in figuring out how I would get this to work how I want? I've literally tried everything: SortableBindingList, Behaviors, Moving items instead of a Refresh, the list goes on. I'm growing gray hairs overhere 😛

Some extra information: I need the Refresh to force the CollectionView to reorder its items. If there are other methods for this that doesn't confuse the TreeView, I'm all ears too.

by LennardF1989 via /r/csharp

Leave a Reply