Thursday, April 2, 2009

Binding - "The devil is in the detail"

Binding is a powerful mechanism in the .NET Framework. Quoting Chris Anderson, "Binding establishes a relationship between a source and a target. In the case of object binding, the selected item for the source is determined by a property path". To bind data to CLR objects we use properties (simple CLR properties and Dependency Property-based properties).

Take a look at the following example:

(click to enlarge)

In the above example I'm binding the Text Property of the TextBox to the Content in the Button. Now, because Text is a dependency property, the above example is the same as the following:

(click to enlarge)

Notice that I changed the path to the “class-qualified” form of the property identifier. Both the above samples will produce the same result:


However, there's actually a big difference between them. The second example will avoid using CLR reflection to resolve the name "Text" in the binding expression because there's a “class-qualified” identifier. The first example does have a performance hit that can be avoided.

"The devil is in the detail."