2012-12-16 75 views
0

在我的WP7項目中,我使用了嵌套的Observable集合。但是,我的問題是按降序排列這個嵌套集合的嵌套集合。收集排序嵌套的ObservableCollection

數據結構:

public class SubCategory : INotifyPropertyChanged 
{ 
    private string _catName; 
    public string CatName 
    { 
     get { return _catName; } 
     set 
     { 
      _catName = value; NotifyPropertyChanged("CatName"); 
     } 
    } 

    private ObservableCollection<ToDoList> _lists; 
    public ObservableCollection<ToDoList> Lists 
    { 
     get { return _lists; } 
     set 
     { 

      if (_lists != value) 
      { 
       _lists = value; 
       NotifyPropertyChanged("lists"); 
      } 
     } 
    } 

這是嵌套集合

public class ToDoList : INotifyPropertyChanged//, INotifyPropertyChanging 
{ 
    #region NotifyPropertyChanged Members 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion 

    #region NotifyPropertyChanging Members 

    // public event PropertyChangingEventHandler PropertyChanging; 
    /* private void NotifyPropertyChanging(string propertyName) 
    { 
     if (PropertyChanging != null) 
      PropertyChanging(this, new PropertyChangingEventArgs(propertyName)); 
    }*/ 
    #endregion 

    [Column(IsVersion = true)] 
    private Binary _version; 

    private int _ItemId; 
    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] 
    public int ItemId 
    { 
     get { return _ItemId; } 
     set 
     { 
      if (_ItemId != value) 
      { 
       //NotifyPropertyChanging("ItemdId"); 
       _ItemId = value; 
       NotifyPropertyChanged("ItemId"); 
      } 
     } 
    } 

    private string _ItemTitle; 
    [Column] 
    public string ItemTitle 
    { 
     get { return _ItemTitle; } 
     set 
     { 
      if (_ItemTitle != value) 
      { 
       //NotifyPropertyChanging("ItemTitle"); 
       _ItemTitle = value; 
       NotifyPropertyChanged("ItemTitle"); 
      } 
     } 
    } 

    private double _ItemLatitude; 
    [Column] 
    public double ItemLatitude 
    { 
     get { return _ItemLatitude; } 
     set 
     { 
      if (_ItemLatitude != value) 
      { 
       //NotifyPropertyChanging("ItemLatitude"); 
       _ItemLatitude = value; 
       NotifyPropertyChanged("ItemLatitude"); 
      } 
     } 
    } 

    private string _ItemAddress; 
    [Column] 
    public string ItemAddress 
    { 
     get { return _ItemAddress; } 
     set 
     { 
      if (_ItemAddress != value) 
      { 
       //NotifyPropertyChanging("ItemAddress"); 
       _ItemAddress = value; 
       NotifyPropertyChanged("ItemAddress"); 
      } 
     } 
    } 

    private double _ItemLongtitude; 
    [Column] 
    public double ItemLongtitude 
    { 
     get { return _ItemLongtitude; } 
     set 
     { 
      if (_ItemLongtitude != value) 
      { 
       //NotifyPropertyChanging("ItemLongtitude"); 
       _ItemLongtitude = value; 
       NotifyPropertyChanged("ItemLongtitude"); 
      } 
     } 
    } 

    private string _ItemDescription; 
    [Column] 
    public string ItemDescription 
    { 
     get { return _ItemDescription; } 
     set 
     { 
      if (_ItemDescription != value) 
      { 
       //NotifyPropertyChanging("ItemDescription"); 
       _ItemDescription = value; 
       NotifyPropertyChanged("ItemDescription"); 
      } 
     } 
    } 

    private bool isScheduled; 
    [Column] 
    public bool IsScheduled 
    { 
     get { return isScheduled; } 
     set 
     { 
      if (isScheduled != value) 
      { 
       //NotifyPropertyChanging("IsScheduled"); 
       isScheduled = value; 
       NotifyPropertyChanged("IsScheduled"); 
      } 
     } 
    } 

    private string _reminderId; 
    [Column] 
    public String ReminderId 
    { 
     get { return _reminderId; } 
     set 
     { 
      if (_reminderId != value) 
      { 
       //NotifyPropertyChanging("ReminderId"); 
       _reminderId = value; 
       NotifyPropertyChanged("ReminderId"); 
      } 
     } 
    } 

    private String _ItemTime; 
    [Column] 
    public String ItemTime 
    { 
     get { return _ItemTime; } 
     set 
     { 
      if (_ItemTime != value) 
      { 
       //NotifyPropertyChanging("ItemTime"); 
       _ItemTime = value; 
       NotifyPropertyChanged("ItemTime"); 
      } 
     } 
    } 
    private bool _isComplete; 
    [Column] 
    public bool IsComplete 
    { 
     get { return _isComplete; } 
     set 
     { 
      if (_isComplete != value) 
      { 
       //NotifyPropertyChanging("IsComplete"); 
       _isComplete = value; 
       NotifyPropertyChanged("IsComplete"); 
      } 
     } 
    } 
    private char _importance; 
    [Column] 
    public char Importance 
    { 
     get { return _importance; } 
     set 
     { 
      if (_importance != value) 
      { 
       //NotifyPropertyChanging("Importance"); 
       _importance = value; 
       NotifyPropertyChanged("Importance"); 
      } 
     } 
    } 
    [Column] 
    internal int _categoryId; 

    private EntityRef<Category> _category; 
    public Category ToDoCategory 
    { 
     get { return _category.Entity; } 
     set 
     { 
      //NotifyPropertyChanging("ToDoCategory"); 
      _category.Entity = value; 
      if (value != null) 
      { 
       _categoryId = value.CategoryId; 
      } 
      //NotifyPropertyChanging("ToDoCategory"); 
     } 
    } 
} 

回答

0

我通過改變get方法解決。這是我的方式。

public ObservableCollection<ToDoList> Lists 
    { 
     get 
     { 
      var l = from ToDoList lis in _lists 
        orderby lis.ItemId descending 
        select lis; 
      return new ObservableCollection<ToDoList>(l); 
     } 
     set 
     { 

      if (_lists != value) 
      { 
       _lists = value; 
       NotifyPropertyChanged("lists"); 
      } 
     } 
    }