I love WPF

"jamais sans son interface"

I love WPF

"jamais sans son interface"

Alternative for ICollectionView (for sorting update)

When you want to sort a collection in MVVM you use

ICollectionView view = CollectionViewSource.GetDefaultView(Items);
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription("DirtyName", ListSortDirection.Descending));

but if you want to update sorting in treeview for sample you can use

ListCollectionView view = new ListCollectionView(collection);
view.IsLiveSorting = true;
SortDescription sort = new SortDescription("DirtyName", ListSortDirection.Ascending);
view.SortDescriptions.Add(sort);

and set IsLiveSorting to true

for sample see converter using in TreeView for update sorting hierarchical

public class TreeViewSortConverter : IValueConverter
{
    public string SortName { get; set; } = null;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        System.Collections.IList collection = value as System.Collections.IList;
        ListCollectionView view = new ListCollectionView(collection);
        view.IsLiveSorting = true;
        SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending);
        view.SortDescriptions.Add(sort);

        return view;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

and using

<TreeViewSortConverter x:Key="tvsc" SortName=""/>
......
<TreeView ItemsSource="{Binding Nodes,
                            Converter={StaticResource tvsc},
                            ConverterParameter=NameDirty}"
.....
<HierarchicalDataTemplate DataType="{x:Type local:Node}"
                          ItemsSource="{Binding Items, 
                                                Converter={StaticResource tvsc},
                                                ConverterParameter=NameDirty}">
.....

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut