Creating a custom (templated) control
User controls are great for encapsulating a piece of UI functionality that can be easily reused. Their potential disadvantage is the lack of deep customization. In case such customization is required (or at least anticipated), a custom control should be built. This is a class deriving from Control
, that provides a default look (through a control template), but that template can be changed if needed, without harming the control's functionality. In fact, this is how all WPF controls work – they provide some default template (look), but we can replace that template while preserving the control's behavior, as we've seen in Chapter 8, Styles, Triggers, and Control Templates.
In this recipe, we'll take a look at creating a custom control, and highlight the differences with respect to a user control.
Getting ready
Open the CH10.UserControls
project from the Creating a user control recipe. We'll use that as a reference and even copy some things that don't need...