【问题标题】:Observable Collection Notify when property changed in MVVM当 MVVM 中的属性更改时通知可观察集合
【发布时间】:2013-02-03 02:27:49
【问题描述】:

我正在尝试将 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 != _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>

【问题讨论】:

    标签: mvvm observablecollection inotifypropertychanged inotifycollectionchanged


    【解决方案1】:

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

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

    例如

     public class studentModel : INotifyPropertyChanged
       .....
    

    【讨论】:

      猜你喜欢
      • 2011-09-30
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 2017-03-13
      • 2015-10-29
      • 1970-01-01
      相关资源
      最近更新 更多