Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1052991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:10:56+00:00 2026-05-16T17:10:56+00:00

Do ControlTemplates in WPF require a TargetType? I am restyling some controls, and notice

  • 0

Do ControlTemplates in WPF require a TargetType? I am restyling some controls, and notice that the comboboxitem, listiviewitem and listboxitem all have the same template:

    <ControlTemplate x:Key="ListBoxItemCT" TargetType="{x:Type ListBoxItem}">

    <Border x:Name="Bd" 
        SnapsToDevicePixels="true" 
        Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}" 
        Padding="{TemplateBinding Padding}"
        CornerRadius="1">
        <ContentPresenter x:Name="cpItemContent"
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
            />
    </Border>

</ControlTemplate>

Is it possible to just remove the TargetType and have one template for all three? I’m trying to do this but get strange errors and problems. I can’t find any specific reference that ControlTemplates must have a type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:10:57+00:00Added an answer on May 16, 2026 at 5:10 pm

    There isn’t a requirement for a TargetType, but if you don’t specify one it will behave the same as if you specify a TargetType of Control.

    • The main advantage that specifying a type gives you is access to all
      of that type’s Dependency Properties in things like TemplateBindings
      and Triggers without having to qualify the property with the owner
      type.

    • Without a TargetType you can also lose implicit bindings, like
      ContentPresenter to the ContentControl.Content property.

    Once you do specify a TargetType that template can only be applied to controls of that type or derived from that type. To share between different types just specify a common base class – ContentControl in this case.

    The following simple templates will give the same basic result but the first is preferable and more common:

    <ControlTemplate x:Key="CommonContentTemplate" TargetType="{x:Type ContentControl}">
        <Border x:Name="Bd" 
                SnapsToDevicePixels="true" 
                Background="{TemplateBinding Background}" 
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}" 
                Padding="{TemplateBinding Padding}"
                CornerRadius="1">
            <ContentPresenter x:Name="cpItemContent"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
        </Border>
    </ControlTemplate>
    

    Without the type all of the Content properties need to be wired up manually:

    <ControlTemplate x:Key="CommonTemplate">
        <Border x:Name="Bd" 
                SnapsToDevicePixels="true" 
                Background="{TemplateBinding Background}" 
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}" 
                Padding="{TemplateBinding Padding}"
                CornerRadius="1">
            <ContentPresenter x:Name="cpItemContent"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                              Content="{TemplateBinding ContentControl.Content}"
                              ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                              ContentTemplateSelector="{TemplateBinding ContentControl.ContentTemplateSelector}"
                              ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"/>
        </Border>
    </ControlTemplate>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a list box in WPF I have this template: <ControlTemplate x:Key=ListBoxItemControlTemplate1 TargetType={x:Type ListBoxItem}>
I have the following ControlTemplate for a WPF TabItem: <ControlTemplate x:Key=DefaultTabItemTemplate TargetType={x:Type TabItem}> <ControlTemplate.Resources>
So I have this WPF style: <Style x:Key=SmallLinkButton TargetType=Button> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=Button>
Suppose you have WPF Window composed of many elements that are using DataTemplates /
I am creating some custom controls in WPF and need to therefore create the
I'm writing several WPF user controls that need both shared and individual resources. I
We have Templates, Control Templates and Data Templates for WPF controls. I don't know
I have just implemented these ToggleButtons using ControlTemplates that set the Image content according
I have WPF ContextMenu with MenuItems that support custom style. MenuItem is a ToggleButton.
I have a WPF button style below: <Style x:Key=myButtonStyle TargetType={x:Type Button}> <Setter Property=Template> <Setter.Value>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.