Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Inherit a Control Template

Tags:

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.

like image 336
photo_tom Avatar asked Jan 09 '10 18:01

photo_tom


2 Answers

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>
like image 134
Aviad P. Avatar answered Sep 16 '22 11:09

Aviad P.


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

like image 35
Oliamster Avatar answered Sep 16 '22 11:09

Oliamster