可观察集合在MVVM中如何实现属性变更通知

11

我正在尝试将可观察集合绑定到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; }
}

而 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>
1个回答

11

ObservableCollection会在记录添加或删除时通知UI,但不会在记录编辑时发出通知。需要由已更改的对象通知其已更改。

在您的情况下,当行被修改时,被更改的对象类型是studentModel
因此,如果您希望UI在该对象更改时收到通知,则需要在studentModel实现INotifyPropertyChanged

例如:

 public class studentModel : INotifyPropertyChanged
   .....

在StudentModel上实现INotifyPropertyChanged接口后,如何通知包含StudentModel的ObservableCollection或集合的更改? - AriesConnolly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接