среда, 25 мая 2011 г.

How to force the ObservableCollection to notify change when a property of an item changes


To make the DataGrid refresh automatically when the property values of data items in the data source are changed, the data item should implement the INotifyPropertyChanged interface. The collection does no need to notify property change at all. For example:
class SomeClass:INotifyPropertyChanged
{
     public event PropertyChangedEventHanlder PropertyChanged;
     private void OnPropertyChanged(string prop)
     {
          if(PropertyChanged!=null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(prop));
          }
     }
     private string name;
     public string Name
     {
          get {return name;}
          set
          {
              if(name!=value)
              {
                  name=value;
                  OnPropertyChanged("Name");
               }
           }
       }
  }
Hope this helps.

Комментариев нет: