Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Binding does not a dependency property make

, 3 Sep 2012
Rate this:
Please Sign up or sign in to vote.
This simple trick stops the binding gremlins on their tracks, as only a dependency property can be their target.

In WPF/Silverlight we are all accustomed to writing things like Width="{Binding foo}" and Text="{Binding bar}". The little Binding gremlins then actually access foo or bar property of the current data context and put its value into Width or Text.

But last night I ran into a nasty problem: what if I wanted to capture the binding itself, to apply it later in a different data context? I tried to create a dependency property of type Binding, but it did not work. When I wrote <MyClass MyBinding={Binding whatever}" /> in XAML, the little binding gremlins were there again and attempted to get the value of whatever from the current data context, failing miserably. There seemed to be no way to tell them not to do that and just return a Binding object.

But then it hit me: WPF DataGrid columns have a property called Binding that does exactly what I needed: it grabs a binding from XAML and then applies it to the data items of the grid one by one. Careful examination of the DataGridBoundColumn class revealed that Binding is NOT a dependency property, it’s a regular CLR property.

This simple trick stops the binding gremlins on their tracks, as only a dependency property can be their target. Problem solved!

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.
 
Please visit my web site: www.ikriv.com.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 3 Sep 2012
Article Copyright 2012 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid