1. 程式人生 > >C# ObservableCollection集合排序

C# ObservableCollection集合排序

注意:ObservableCollection集合 直接使用OrderBy 等函式排序,有問題的,排序不改變,可以轉成List在進行排序

名稱空間:System.Collections.ObjectModle

在WPF中,繫結列表List,資料更新後不會通知介面更新,要使用ObservableCollection集合,當資料發生改變就通知介面改變;

解決方法一:List列表轉為ObservableCollection集合

private ObservableCollection<BaseSignalViewModel> children = new ObservableCollection<BaseSignalViewModel>();
public ObservableCollection<BaseSignalViewModel> Children
{
    get { return children; }
    set
    {
        children = value;
        NotifyOfPropertyChange(() => Children);
    }
}

List<BaseSignalViewModel> listChild = CurrentSignal.Children.ToList();
listChild.Sort(delegate(BaseSignalViewModel p1, BaseSignalViewModel p2) { return Comparer<string>.Default.Compare(p1.Index.ToString(), p2.Index.ToString()); });
//List列表轉為ObservableCollection集合
CurrentSignal.Children = new ObservableCollection<BaseSignalViewModel>(listChild);