Create a WPF Binding in Code for a simple DependencyObject (not a FrameworkElement nor a ContentFrameworkElement)

FrameworkElement and ContentFrameworkElement classes from WPF both support a simple SetBinding() method which you could use to create bindings to their dependency properties from code. However, when you need to bind a dependency property of a simple dependency object, you will find that SetBinding doesn’t exist, as it’s not defined at the DependencyObject layer. At the first sight, it seems that only FrameworkElement and ContentFrameworkElement support bindings. But fortunately, it’s not the case. You just need to use the BindingOperations.SetBinding method to resolve your problem.

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in Computers and Internet. Bookmark the permalink.

2 Responses to Create a WPF Binding in Code for a simple DependencyObject (not a FrameworkElement nor a ContentFrameworkElement)

  1. 0xdfx says:

    Thanks, got me out of a sticky situation!

  2. Marcelo Augusto says:

    Man, you saved me!

    I was wondering that it would be a non sense to have a Framework Element just to store data e use bindings to change properties values. But now it all makes sense. You helped me a lot.

    Nice post!

Add a reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s