TreeListBox list addition does not trigger custom sorting

Grids for WPF Forum

Posted 1 month ago by Sunshine - Appeon
Version: 21.1.1
Avatar

Example : https://ibb.co/x7GqBGp

I inherited TreeListBoxItemAdapter and overridden the GetChildren method.

This is my code:

public override IEnumerable GetChildren(TreeListBox ownerControl, object item)
        {
            if (item is TreeNode node)
            {
                if (this.IsAutoSorted)
                {
                    var collection = CollectionViewSource.GetDefaultView(node.Children);

                    collection.SortDescriptions.Add(new SortDescription("Order", ListSortDirection.Ascending));

                    collection.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));

                    return collection;
                }
                else
                {
                    return node.Children;
                }
            }
            else
            {
                return Enumerable.Empty<ITreeNode>();
            }
        }

I sort the collection according to Order and Name.

When I add a node, the tree cannot automatically trigger my sorting rules.

I collaspse and then expand, it can trigger the sorting rules.

How to solve this problem so that the sorting is triggered correctly when adding nodes?

Thanks!

Comments (3)

Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

GetChildren is only called when the node is requesting its children for expansion.  Once a node is expanded, it watches the collection returned from GetChildren to see if it notifies of any changes.  If it does, TreeListBox will try and make the appropriate insertion/removal in the same spot indicated by INotifyCollectionChanged.  

Can you watch the view and attach to its CollectionChanged property here to see which index it says to insert the item?  That is effectively the index we use to determine where the inserted node goes.  See if it's the same as what you see resulting in the UI.  I would think that the collection view would insert the new item into a sorted location and that its CollectionChanged event would point to that new sorted index.


Actipro Software Support

Posted 1 month ago by Sunshine - Appeon
Avatar

I can see the index of the inserted item and the sorting in the collection by subscribing to the CollectionChanged event. I found that the sorting in the collection and the inserted index are correct, but the UI display is inconsistent.

Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Ok, in that case, can you reproduce this in a new simple sample project and send that over to our support address?  Please mention this thread in your e-mail and remove the bin/obj folders from the .zip you send so it doesn't get spam blocked.  Then we'll debug with that to see what's happening.  Thanks!


Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.