Expand Wpf Treeview to support Sorting

前端 未结 3 1135
深忆病人
深忆病人 2021-01-21 09:48

Hi I created this small example, and I would like to expand it to support Sorting.

public class Country
{
    public string Name { get; set; }
    public int Sor         


        
3条回答
  •  难免孤独
    2021-01-21 10:11

    You can also use the CollectionViewSource.GetDefaultView with ListCollectionView to do the sorting for you, tho you do have to set it for all the child collections as well. I use a linq query to flatten my list to find all the children and set the sorting.

    foreach (var structure in Model.Structures.Flatten(t => t.SubItems))
    {
        var view = CollectionViewSource
            .GetDefaultView(structure.SubItems) as ListCollectionView;
    
        if (view != null)
        {
            view.CustomSort = (x,y) => string.Compare( x, y );
        }
    }
    

    elsewhere

    public static IEnumerable Flatten(
        this IEnumerable list, Func> subitems)
    {
        foreach (T child in list)
        {
            yield return child;
    
            foreach (T other in Flatten(subitems(child), subitems))
            {
                yield return other;
            }
        }
    }
    

提交回复
热议问题