小编Nit*_*hav的帖子

可观察集合在MVVM中更改属性时通知

我试图将observable集合绑定到DataGrid,我想在datagrid中编辑任何行时通知.我的代码适用于添加或删除记录时的通知,但是在编辑记录时没有通知.如果这是使用MVVM中的可观察集合进行绑定的正确方法,请告诉我,以及我是否缺少某些内容.提前致谢.

public class studentViewModel : INotifyPropertyChanged
{
    #region constructor

    public studentViewModel()
    {
        _student = new studentModel();
        myCollection = new ObservableCollection<studentModel>();
        myCollection.Add(_student);
        myCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(myCollection_CollectionChanged);

    }

    void myCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        //throw new NotImplementedException();
        System.Windows.MessageBox.Show(e.Action.ToString());
    }

    #endregion

    #region properties

    studentModel _student;
    ObservableCollection<studentModel> _myCollection;

    public ObservableCollection<studentModel> myCollection
    {
        get { return _myCollection; }
        set
        {
            if (_myCollection != value)
            {
                _myCollection = value;
                raisePropertyChange("myCollection");
            }
        }
    }

    public int rollNo
    {
        get { return _student.rollNo; }
        set
        {
            if (value …
Run Code Online (Sandbox Code Playgroud)

observablecollection mvvm inotifycollectionchanged inotifypropertychanged

9
推荐指数
1
解决办法
1万
查看次数