Sorting an observable collection with linq

后端 未结 6 1120
囚心锁ツ
囚心锁ツ 2020-12-16 12:13

I have an observable collection and I sort it using linq. Everything is great, but the problem I have is how do I sort the actual observable collection? Instead I just end

6条回答
  •  死守一世寂寞
    2020-12-16 12:37

    If you are using Silverlight 3.0, then using CollectionViewSource is the cleanest way. Refer below example: (it can be done via xaml as well)

    ObservableCollection ecAll = new ObservableCollection();
    CollectionViewSource sortedcvs = new CollectionViewSource();
    sortedcvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Date", 
        System.ComponentModel.ListSortDirection.Ascending));
    sortedcvs.Source = ecAll;
    ListBoxContainer.DataContext = sortedcvs;
    

    And in corresponding xaml set

    ItemsSource="{Binding}"
    

    for the ListBox or any ItemsControl derived control

提交回复
热议问题