22

I working on a WPF project where I've over-ridden the CheckBox control for some special operations. That is working correctly.

My problem is that the ControlTemplate that was applied from the theme (shinyred.xaml from codeplex), is not applied to my over-ridden control. Is there a way to inherit the CheckBox ControlTemplate for use by my new control?

All the samples the I can find are focused on inheriting the style for the CheckBox, but nothing about the ControlTemplate.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
photo_tom
  • 7,292
  • 14
  • 68
  • 116

2 Answers2

23

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>
Aviad P.
  • 32,036
  • 14
  • 103
  • 124
  • 1
    "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." Isn't that normally the case for inheritance? – Jeff Dec 03 '18 at 16:50
  • It is, for types, but this is more like inheriting from a function implementation, not from a type definition. – Aviad P. Dec 04 '18 at 00:12
  • @Jeff it is, but only for public & protected elements of the base class. But it seems like the proper analogy is that the content of the ControlTemplate is "private". – StayOnTarget Apr 25 '19 at 19:48
3

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

Oliamster
  • 486
  • 5
  • 10
  • I'm afraid not. If you what to define your control as UserControl (I mean with the code behind) instead of CustomControl you are in any case forced to inherit from a button or the control for which the template is based. otherwise, when applying let say the ButtonBaseStyle you will have a compilation error. But I will do a try I let you know. – Oliamster May 05 '20 at 23:55
  • That's unfortunate it'd be great if there was a way. If anything UserControl should be treated as a base for us to all inherit from in code behind. – Hawkeye4040 May 07 '20 at 05:19
  • 1
    This is one of best solutions I've ever seen. It's practical to keep the default button style and behaviors. No need to deal with animations, triggers, states. – aliassce Jun 26 '20 at 00:41