Be smart with data binding

WPF’s data binding is so sweet that it should not be tied to the use of merely binding the UI to the data. Generically speaking, as long as it is dependency property, it can be data-bound. So, here is another situation where you would want to use data binding as well.

Say, you want a property of a object to automatically update whenever its source changes. The very decent solution will be that you turn the target property to dependency property, and bind it to the source. To make the source able to notify the target of any change, you can either implement IPropertyChangeNotification, or wrap the source to a dependency property. In this way, the target property does not need any logic to keep itself updated.

So, next time you need to assign a value to a dependency property, make sure if you want its value to automatically update. If you do, now you know what you can also do.

One thing that needs to be paid attention to, though, is that you should remember to clear the bindings whenever the object is not needed any more. A good place to clear bindings is the Dispose method.


~ by Martin on December 11, 2008.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: