8

我試圖將可觀察集合綁定到DataGrid,我想在DataGrid中編輯任何行時通知。 我的代碼工作正常,當記錄添加或刪除,但它不記錄編輯記錄時通知。 請讓我知道,如果這是使用MVVM中的observable集合綁定的正確方法,並且我是否缺少someting。提前致謝。Observable Collection在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> 

回答

7

一個ObservableCollection將通知當一個記錄被添加或刪除,但當一個記錄被編輯的UI。這取決於已更改的對象以通知它已更改。

在你的情況下,當一行被修改時,被改變的對象的類型是studentModel
因此,如果要在該對象被改變得到通知的UI,你需要implement INotifyPropertyChangedstudentModel太..

例如

public class studentModel : INotifyPropertyChanged 
    .....