Alternative for ICollectionView (for sorting update)
When you want to sort a collection in MVVM you use
1 2 3 |
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
1 2 3 4 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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
1 2 3 4 5 6 7 8 9 10 11 |
<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}"> ..... |
Commentaires récents