Monday, July 15, 2013

Walkthrough: Two-way binding inside a XAML User Control

User controls are a great way couple logic and XAML in an encapsulated way. You can reuse a user control over and over and the logic is isolated from the general flow of your program. This article is not about creating a user control. This article is about binding to a user control.


A user control contrasts with a custom control in that a custom control contains no XAML. Custom control can be (and is sometimes are required to be) templated (for example, a DataTemplate). Conversely, user controls cannot be templated as the XAML is embedded.

The simplest user control might look like this:

Inline image 1


The simplest use of a user control would be like this:

Inline image 2

In the code above, I simply declare the user control and the rest is done for me. What isn't done in the code is passing information to set the value of any properties or custom properties.

Please note: the data context property of the user control inherits from the parent. A DataTemplate might like this. But user controls don't anticipate a data context type. Instead, they want properties explicitly set. And we want to bind those properties.

Read more: Jerry Nixon blog
QR: Inline image 3