how to sort ObservableCollection

前端 未结 5 671
轮回少年
轮回少年 2020-12-08 06:00

I have a an ObservableCollection and a WPF UserControl is Databound to it. The Control is a graph that shows a vertical bar for each item of type BarData in the ObservableCo

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 06:33

    Also using LINQ/Extensionmethod one can aviod firing the NotifyPropertyChanged Event by not setting the source col to the sorted one, but clear the original and add the items of the sorted one. (this will continue fire the Collectionchanged Event, if implemented).

    
    Public Sub SortByProp(Of T)(ByRef c As ICollection(Of T), PropertyName As String)
        Dim l = c.ToList
        Dim sorted = l.OrderBy(Function(x) x.GetType.GetProperty(PropertyName).GetValue(x))
    
        c.Clear()
        For Each i In sorted
            c.Add(i)
        Next
    
    End Sub
    

提交回复
热议问题