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!eProject