How to Inherit a Control Template How to Inherit a Control Template wpf wpf

How to Inherit a Control Template


No, as you said it is possible to 'inherit' a style by using the BasedOn property, but it's not possible to directly 'inherit' a template. This is understandable though, what would be the semantics of template inheritance? How would the derived template be able to somehow add or change elements in the base template?

With styles it's entirely possible, since you can simply add Setters, Triggers, etc. The only thing that would conceivably be possible with template inheritance is adding Triggers to the base template. However, in that case you'd have to have intimate knowledge of the element names in the base template, and an element name change in the base template could break your derived one. Not to mention an issue with readability, where you refer to a name in your derived template, which is defined somewhere else entirely.

Belated Addition Having said all that, it is possible to resolve your particular problem (although I doubt by now it is still yours, or even a problem). You simply define a style for your control with a setter for the Template property thus:

<Style TargetType="<your type>">    <Setter Property="Template" Value="{StaticResource <existing template resource name>}"/></Style>


Keeping in mind what said by @Aviad, the following is a work around:

say you have a Button that define a template that you want to ihnerit, define your CustomButton as Custom Control like this:

public class CustomButton : Button{    static CustomButton()    {        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));    }    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",        typeof(string),  typeof(CustomButton), new UIPropertyMetadata(null));    public string Text    {        get { return (string)GetValue(TextProperty); }        set { SetValue(TextProperty, value); }    }}

Then go to your Generic.xaml and define the following:

 <Style    x:Key="CustomButtonStyle" TargetType="{x:Type local:CustomButton}">    <Setter Property="FontSize" Value="18" /> <!--Override the font size -->    <Setter Property="FontWeight" Value="Bold" />    <Setter Property="Template">        <Setter.Value>            <ControlTemplate TargetType="{x:Type local:CustomButton}">                <Button Style="{StaticResource ButtonStyleBase}"                     Height="{TemplateBinding Height}"                         Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:CustomButton}}, Path=Command}"                        CommandParameter="{Binding}"                        Width="{TemplateBinding Width}">                    <Grid>                        <StackPanel>                            <Image Source="Image/icon.jpg" />                            <TextBlock Text="{TemplateBinding Text}"></TextBlock>                        </StackPanel>                    </Grid>                </Button>            </ControlTemplate>        </Setter.Value>    </Setter></Style>

Note that the button we want to inherit the template is wrapped inside my new template, and the style is set to the existing button. go the same way with the checkbox and organize the checkbox and label for instance vertically inside the new ControlTemplate of the CustomCheckBox