8

私はObservableコレクションをDataGridにバインドしようとしていますが、DataGridで行が編集されたときに通知する必要があります。 レコードが追加または削除されたときに、レコードが編集されたときに通知されません。 MVVMで観測可能なコレクションを使用してこれが正しいバインド方法であるかどうか、そして私に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

は、レコードが追加または削除されますが、 は、レコードが編集されていないたときにされたUIを通知します

<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です。変更されたことを通知するために変更されたのは、オブジェクトに依存します。

行が変更された場合、変更されるオブジェクトのタイプはstudentModelです。
あなたは、そのオブジェクトが変更されたときにUIが通知を受けるにしたい場合はそのため、あなたはあまりにもstudentModelimplement INotifyPropertyChangedに必要..

例えば

public class studentModel : INotifyPropertyChanged 
    ..... 
関連する問題