Templates in WPF are used to customize the UI. We can manipulate both controls as well as data.
Every control in WPF has its default template associated with it. Default template defines a look and feel, basically a style of control.
That's why by default, Button or TextBox shapes are ectangular because it is defined in its default template. Now we can update that template and add our own implementation.
In WPF there are 2 types of Templates,
- Control Template: Customize the appearance
- Data Template: Customize the functionality.
We can define these templates inside App.xaml or in Resource file to reuse them in the application.
Control Template
So by default, this is how the Button looks.
- <Button x:Name="SubmitButton"
- Content="Submit"
- Height="20"
- Width="100"/>
Let's create a custom control to change the shape of this button.
Let's add a ControlTemplate inside a Button to achieve this. But you might get the following error.

As you can see .net framework is giving us compiled time error, specifying Visual tree can only be set once, meaning that ControlTemplate can only have
one child. But we have a couple of tags inside a ControlTemplate.
one child. But we have a couple of tags inside a ControlTemplate.
- <Button x:Name="SubmitButton"
- Content="Submit"
- Height="20"
- Width="100">
- <Button.Template>
- <ControlTemplate>
- <Ellipse Fill="Gray"/>
- <ContentPresenter/>
- </ControlTemplate>
- </Button.Template>
- </Button>
So to overcome this problem, we can add Panel inside ControlTemplate and add any number of tags inside that grid, because grid can have multiple children.
- <Window x:Class="A.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:A"
- mc:Ignorable="d"
- Title="MainWindow" Height="150" Width="300">
- <Grid x:Name="MainGrid">
- <Button x:Name="SubmitButton"
- Foreground="White"
- Height="20"
- Width="100">
- <Button.Template>
- <ControlTemplate x:Name="ButtonElipse">
- <Grid>
- <Ellipse Fill="Gray"/>
- <ContentPresenter
- Content="Submit"
- VerticalAlignment="Center"
- HorizontalAlignment="Center"/>
- </Grid>
- </ControlTemplate>
- </Button.Template>
- </Button>
- </Grid>
- </Window>



As you can see, new ControlTemplate has been assigned to a Button.
Now let's load our ControlTemplate in ResourceDictionary and fetch it from there.
ResourceDictionaryTemplate.xaml
- <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:A">
- <Thickness x:Key="MarginTR">0 5 5 0</Thickness>
- <ControlTemplate x:Key="EllipseButton">
- <Grid>
- <Ellipse Fill="Gray"/>
- <ContentPresenter
- Content="Submit"
- VerticalAlignment="Center"
- HorizontalAlignment="Center"/>
- </Grid>
- </ControlTemplate>
- </ResourceDictionary>
MainWindow.xaml
First, you need to add Window.Resources.
Second use Tag Template to bind the EllipseButton ControlTemplate.
- <Window x:Class="A.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:A"
- mc:Ignorable="d"
- Title="MainWindow" Height="150" Width="300">
- <Window.Resources>
- <ResourceDictionary Source="ResourceDictionaryTemplate.xaml"/>
- </Window.Resources>
- <Grid x:Name="MainGrid">
- <Button x:Name="SubmitButton"
- Foreground="White"
- Template="{StaticResource EllipseButton}"
- Height="20"
- Width="100"/>
- </Grid>
- </Window>


Join the conversation! Your thoughts help the community grow.