In this situation there are several options open to you - you could hijack a property - bad idea when it comes to maintainability. Alternatively you could inherit the control and introduce the new properties - again, not the most ideal situation under WPF's composition model.
By far the most flexible and easiest way to achieve this behaviour is with attached dependency properties.
In the following example, we want to extend all of our buttons to provide not just a single line of text as you'd find on normal buttons, but we also want to have customisable sub-title line. To achieve this we start with the custom dependency property;
1 |
public class ButtonPropertyExtender : DependencyObject
|
Quite simply this registers a custom attached property with WPF and allows it to be attached to any element. The next thing we do is use the new property on our button thus;
1 |
<Button xc:ButtonPropertyExtender.SubText="Subtitle text">
|
And finally, to use our new property in the control template, we bind to it - notice however how we cannot use the {TemplateBinding} shortcut - instead we must use the full binding expression to get to the new custom property.
1 |
<ControlTemplate TargetType="Button">
|