0

在我的 WP7 项目中,我使用嵌套的 Observable 集合。但是,我的问题是使用它的嵌套集合按降序对这个嵌套集合进行排序。

集合的数据结构:

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");
        }
    }
}
4

1 回答 1

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");
            }
        }
    }
于 2012-12-16T11:29:06.267 回答