Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / WPF

Binding Does Not a Dependency Property Make

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Sep 2012Apache1 min read 6.3K   3  
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!

This article was originally posted at http://www.ikriv.com/blog?p=1069

License

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


Written By
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 --