可观察的集合在MVVM中的属性更改时通知 [英] Observable Collection Notify when property changed in MVVM

查看:141
本文介绍了可观察的集合在MVVM中的属性更改时通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将可观察的集合绑定到DataGrid,我想通知何时在datagrid中编辑任何行. 我的代码可以很好地在添加或删除记录时进行通知,但在编辑记录时不进行通知. 请让我知道这是否是使用MVVM中的可观察集合进行绑定的正确方法,以及我是否缺少某些东西.预先感谢.

I am trying to bind observable collection to DataGrid, i want to notify when any row is edited in datagrid. My code works fine for notifing when record is added or removed but its not notifing when record is edited. Please let me know if this is the right way of binding using observable collection in MVVM and whether i am missing someting. Thanks in advance.

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; }
}

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,但是在编辑记录时,不是.通知已更改的对象取决于已更改的对象.

An ObservableCollection will notify the UI when a record is added or removed but not when a record is edited. It's up to the object that has been changed to notify that it has changed.

在您的情况下,修改一行后,更改的对象类型为studentModel.
因此,如果您希望UI在更改对象时得到通知,则需要实现INotifyPropertyChanged 也在studentModel上.

In your case, when a row is modified, the type of object which is changed is studentModel.
Therefore, if you want the UI to get notified when that object is changed, you need to implement INotifyPropertyChanged on studentModel too..

例如

 public class studentModel : INotifyPropertyChanged
   .....

这篇关于可观察的集合在MVVM中的属性更改时通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆