locked
TargetType duplication in Control Templates RRS feed

  • Question

  • Say, I have this Button template:

    <Style TargetType="Button">
    	<Setter Property="Template">
    		<Setter.Value>
    			<ControlTemplate TargetType="Button">
    				<Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="Red" TextBlock.Foreground="White">
    					<ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}" />
    				</Border>
    			</ControlTemplate>
    		</Setter.Value>
    	</Setter>
    </Style>

    It works well and good, but as soon as I remove TargetType property of ControlTemplate, the text in the button disappears, and everything else remains intact. So, I wonder how removing affects text and why I can't avoid TargetType duplication since I already denoted it in Style. Thanks.


    There is no knowledge that is not power.


    • Edited by JohnyL Sunday, March 6, 2016 8:01 AM
    Saturday, March 5, 2016 7:53 PM

Answers

  • The reason is the way datatemplating works.

    You're saying your style will be applied to a Button.

    Ok this is all for a button and must try and set properties a button has.

    Then you tell it to set the Template.

    It knows there's a template property of a button and it will take whatever you give it as a Template.

    Then you tell it to set a template it's just any old sort of a template as far as xaml is concerned.

    It'll add it to it's list of templates.

    If you want to apply it to anything then you have to give it a type to match against.

    This last step is your problem if you miss out the TargetType since it's that says when you find a button use this ControlTemplate for it. Without that it would have a control template but not really know what to use that on.

    If this is still confusing then bear in mind what you think of as one controls is often built from several. Like a Combobox which is a togglebutton, textbox, popup and a listbox.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Sunday, March 6, 2016 2:59 PM

All replies

  • In the style you are declaring what object type the style is defined for.

    In the ControlTemplate you are declaring what object type the Template is defined for.

    You could (although I don't know why) define a different type for each but then you get a parsing error.  

    As soon as you remove the Template targettype the XAML no longer knows what is in the ContentPresenter (which is where you will see your Content text) so you will get no "Button" parts in the ContentPresenter.


    Lloyd Sheen

    Saturday, March 5, 2016 9:23 PM
  • I don't get this moment. The Style has target type Button. So, shouldn't it be obvious that if the style is for button, then the Template is for Button too?

    There is no knowledge that is not power.

    Sunday, March 6, 2016 8:05 AM
  • Its just the syntax.  The Style is for a particular type defined by TargetType.  The Template is for a particular type defined by TargetType.  XAML has no sense of obvious.  You may think that but remember that in a Style you don't have to set the Template and vice versa  you can set a ControlTemplate which is not in a Style.

    Not really sure why you are having a problem with this.


    Lloyd Sheen

    Sunday, March 6, 2016 2:47 PM
  • The reason is the way datatemplating works.

    You're saying your style will be applied to a Button.

    Ok this is all for a button and must try and set properties a button has.

    Then you tell it to set the Template.

    It knows there's a template property of a button and it will take whatever you give it as a Template.

    Then you tell it to set a template it's just any old sort of a template as far as xaml is concerned.

    It'll add it to it's list of templates.

    If you want to apply it to anything then you have to give it a type to match against.

    This last step is your problem if you miss out the TargetType since it's that says when you find a button use this ControlTemplate for it. Without that it would have a control template but not really know what to use that on.

    If this is still confusing then bear in mind what you think of as one controls is often built from several. Like a Combobox which is a togglebutton, textbox, popup and a listbox.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Sunday, March 6, 2016 2:59 PM