I have a DataGridView that is bound to a collection. The types in the collection implement INotifyPropertyChanged (textbook implementation from the MSDN page).
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public string Name
{
get { return m_Name; }
set { m_Name = value; NotifyPropertyChanged("Name"); }
}
I’m trying to understand when, how and why the PropertyChanged event actually gets fired. If I write code to use the Name property to change the string everything works, PropertyChanged is != NULL, and my DataGridView updates correctly. Like so:
for (int i = 0; i < Server.Customers.Count; i++)
{
Server.Customers[i].Name = Server.Customers[i].Name + "!!";
}
That’s just a test, however, the way the collection should really update is via XML deserialization. The implementation for the serializer is very straighforward, and the code steps through the exactly the same Name property (calling NotifyPropertyChanged) as in the previous example. With one difference: PropertyChanged turns out to be NULL and is never invoked. Result: no update in my data binding.
I don’t quite understand what’s going on here. I never explicitly subscribe to PropertyChanged in the first place (and neither do any of the code examples I have found), yet it gets invoked correctly in the first example. How do I get this to work for my second example, where I deserialize my XML into the object?
XML serialization can’t update an existing object, it always creates a new one when you deserialize. Since it’s a new object, there isn’t any handler for
PropertyChangedyet, so the event isn’t fired.It doesn’t make sense to listen to the
PropertyChangedevent of an object that is being created. What are you trying to do exactly?