8

में संपत्ति बदलते समय सूचित करें मैं डेटाग्रिड में देखने योग्य संग्रह को बांधने की कोशिश कर रहा हूं, मैं डेटाग्रिड में किसी भी पंक्ति को संपादित करते समय सूचित करना चाहता हूं। मेरा कोड नोटिफ़िक करने के लिए ठीक काम करता है जब रिकॉर्ड जोड़ा जाता है या हटा दिया जाता है लेकिन रिकॉर्ड संपादित होने पर यह नोटिफ़िक नहीं होता है। कृपया मुझे बताएं कि क्या यह एमवीवीएम में देखने योग्य संग्रह का उपयोग करके बाध्यकारी का सही तरीका है और क्या मुझे कभी याद आ रही है। अग्रिम में धन्यवाद।पर्यवेक्षण संग्रह 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 है यूआई जब एक रिकार्ड जोड़े या निकाले लेकिन एक रिकॉर्ड संपादित किया जाता है जब नहीं सूचित कर देंगे। यह उस ऑब्जेक्ट पर निर्भर है जिसे यह सूचित करने के लिए बदला गया है कि यह बदल गया है।

आपके मामले में, जब कोई पंक्ति संशोधित होती है, तो ऑब्जेक्ट का प्रकार बदल जाता है studentModel है।
इसलिए, अगर आप चाहते हैं जब उस वस्तु बदल गया है यूआई सूचना पाने के लिए, आप implement INotifyPropertyChanged को studentModel भी पर की जरूरत है ..

उदा

public class studentModel : INotifyPropertyChanged 
    ..... 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^