开发者

Two-way databinding to POCO without INotifyPropertyChanged

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 开发者_如何学Cimplement 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜