views:

105

answers:

2

How does two-way databinding work in Silverlight when the data source object's properties don't raise PropertyChanged events in their setters?

For example, in code samples, I've seen databinding to instances of the System.Windows.Point struct, which does not implement INotifyPropertyChanged and is mutable. What happens (or should happen) if someone sets the X and Y properties directly in the Point, rather than replacing the object with a new instance?

+3  A: 

The UI does not update. There is no magic here. No event thrown means the UI will miss the update.

TomTom
+2  A: 

Point is a struct, so even though Point is mutable, the Point you get from the property call is not the same as the Point stored in the underlying field; it's a copy. As such, if you mutate the copy, the underlying field remains the same. There is no need for a property changed notification, because the value of the property has not actually changed. There would only be a problem if the class actually mutated the Point in its private field directly. It's up to the class implementer to either not do this or manually invoke the PropertyChanged notification when the struct is mutated.

This is one reason why mutable structs are dangerous. They cannot be mutated through a property, but clients of the class may incorrectly assume that they can be.

Dan Bryant