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

在MVVM中更改属性时通知可观察集合,mvvm,observablecollection,inotifypropertychanged,inotifycollectionchanged,Mvvm,Observablecollection,Inotifypropertychanged,Inotifycollectionchanged,我正在尝试将可观察集合绑定到DataGrid,我希望在DataGrid中编辑任何行时发出通知。 我的代码可以在添加或删除记录时进行通知,但在编辑记录时不进行通知。 请让我知道这是否是在MVVM中使用可观察集合进行绑定的正确方法,以及我是否遗漏了一些内容。提前谢谢 public class studentViewModel : INotifyPropertyChanged { #region constructor public studentViewModel() {

我正在尝试将可观察集合绑定到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 != _student.rollNo)
            {
                _student.rollNo = value;
                raisePropertyChange("rollNo");
            }
        }
    }

    public string firstName
    {
        get { return _student.firstName; }
        set
        {
            if (value != _student.firstName)
            {
                _student.firstName = value;
                raisePropertyChange("firstName");
            }
        }
    }

    public string lastName
    {
        get { return _student.lastName; }
        set
        {
            if (value != _student.lastName)
            {
                _student.lastName = value;
                raisePropertyChange("lastName");
            }
        }
    }

    #endregion

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;


    public void raisePropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
           PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

public class studentModel
{
    public int rollNo { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
}
公共类studentViewModel:INotifyPropertyChanged
{
#区域构造函数
公共学生视图模型()
{
_学生=新学生模型();
myCollection=新的ObservableCollection();
myCollection.Add(_学生);
myCollection.CollectionChanged+=新系统.Collections.Specialized.NotifyCollectionChangedEventHandler(myCollection\u CollectionChanged);
}
void myCollection\u CollectionChanged(对象发送方,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
//抛出新的NotImplementedException();
System.Windows.MessageBox.Show(例如Action.ToString());
}
#端区
#区域属性
学生模型;
可观察采集(myCollection);;
公共可观测集合myCollection
{
获取{return\u myCollection;}
设置
{
如果(_myCollection!=值)
{
_myCollection=value;
raisePropertyChange(“myCollection”);
}
}
}
公共int rollNo
{
获取{return\u student.rollNo;}
设置
{
如果(值!=\u student.rollNo)
{
_student.rollNo=值;
raisePropertyChange(“rollNo”);
}
}
}
公共字符串名
{
获取{return\u student.firstName;}
设置
{
如果(值!=\u学生名)
{
_student.firstName=值;
raisePropertyChange(“名字”);
}
}
}
公共字符串姓氏
{
获取{return\u student.lastName;}
设置
{
如果(值!=\u student.lastName)
{
_student.lastName=值;
raisePropertyChange(“姓氏”);
}
}
}
#端区
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
public void raisePropertyChange(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
}
公共班级学生模式
{
public int rollNo{get;set;}
公共字符串名{get;set;}
公共字符串lastName{get;set;}
}
xaml是

<Window.Resources>
    <local:studentViewModel x:Key="StudentsDetails" />
</Window.Resources>
<Grid DataContext="{StaticResource StudentsDetails}">
    <DataGrid ItemsSource="{Binding Path=myCollection, Source={StaticResource StudentsDetails}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              Name="studentsGrid" CanUserAddRows="True" AutoGenerateColumns="True">

    </DataGrid>
</Grid>

当添加或删除记录时,ObservableCollection将通知UI,但当编辑记录时,不会通知UI。由已更改的对象通知它已更改

在您的情况下,修改行时,更改的对象类型为
studentModel

因此,如果您想在该对象发生更改时通知UI,则还需要打开
studentModel

e、 g

 public class studentModel : INotifyPropertyChanged
   .....