TreeListBox - Sorting items automatically on rename

Grids for WPF Forum

Posted 7 years ago by Thomas Dupuis
Version: 17.1.0652
Avatar

Hello,

We are using the new TreeListBox and, while we like it a lot so far, we have the following problem:
We want to sort items in our tree, with custom sort rules. And we want this custom sorting to be applied all the time (even right after renaming a node)

We have our own TreeListBoxItemAdapter override, and our GetChildren override looks like this:

public override IEnumerable GetChildren(TreeListBox ownerControl, object item)
{
var dataExplorerViewModel = item as DataExplorerViewModel;
if (dataExplorerViewModel != null)
return dataExplorerViewModel.SortedChildren;

var treeItem = item as TreeItem;
return treeItem?.SortedChildren;
}       

 With sortedChildren being:

public IEnumerable SortedChildren
{
    get
    {
        var collectionView = CollectionViewSource.GetDefaultView(Children) as ListCollectionView;

        if (collectionView != null)
            collectionView.CustomSort = Comparer;

        return collectionView;
    }
}

It works great but when we rename one item in our explorer, the sorting is not refreshed. To force a new sort of the view, we currently use the following code after each tree item name modification:

var collection = CollectionViewSource.GetDefaultView(Parent.Children);
Dispatcher.CurrentDispatcher.InvokeAsync(() => collection.Refresh());

1. It looks like we are doing the job the control could do.

2. By doing it this way, we are losing the current selection in the tree.

3. This "refresh the default view" solution doesn't look super optimized and we don't want to face issues later in our project, when tree items will be modified frequently by other users (push)

Do you have any advice to handle this use case?

Thanks a lot. 

[Modified 7 years ago]

Comments (5)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Thomas,

Thank you for writing.  We are happy to brainstorm ideas with you to try and improve things.

I'm assuming that the collection.Refresh() call is triggering a CollectionChanged event with "Reset" action set.  That will remove the old items in the collection and then will re-add the items from the new collection.  I could see the selection getting lost at that point if it was previously one of the old items.

What would you ideally like to see API-wise from us that would help with the various scenarios you described?

With the existing codebase, the only advice I have would be to perhaps see if any of the items have an IsSelected flag set before the collection is refreshed.  Then restore that property again if the item remains in the collection after refresh.


Actipro Software Support

Posted 7 years ago by Thomas Dupuis
Avatar

Hi, 

Yes, you are assuming correctly.

I wanted to make sure we don't miss any cool feature from your control. But through your reply, it looks like our way of handling treenode renaming doesn't surprise you. (We can fix our small selected item issue)

I was just surprised to have to handle the refreshing part (why bother with bindings then? ;))

After more research, I'm going to have a look at IEditableCollectionView, and at this.
I might find something better than a manual force refresh.

As always, thank you for your fast reply!

[Modified 7 years ago]

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Thomas,

When we watch the child items source for a node, we basically look for it to implement INotifyCollectionChanged, and we watch for the CollectionChanged event.

If you could provide us with a small sample using a collection that just triggers Move actions to the CollectionChanged event in this case, we could use that and possibly enhance our logic to not lose the selection.  Right now, a Move in our handler will act like a full Reset.  If you would like to send a sample, please rename the .zip file extension and exclude bin/obj folders, and send it to our support address.  Reference this thread in your email.  Thanks!


Actipro Software Support

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Thomas,

Thank you for submitting the sample via your colleague.  We've made some updates for the next 2017.2 build that should help with this.


Actipro Software Support

Posted 7 years ago by Thomas Dupuis
Avatar

Hello Actipro, 

I confirm your 2017.2 build solves our issue.
Thank you very much!

[Modified 7 years ago]

The latest build of this product (v24.1.2) was released 0 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.