Posts Control Template for a button
Post
Cancel

Control Template for a button

Output

In this example, a control template is set explicitly on a button to change the way it appears.

window1.xaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<Window x:Class="ButtonTemplate0.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Button" Height="80" Width="150">
   <Window.Resources>
      
      <ControlTemplate x:Key="myButtonTemplate">
         <Border Padding="5" Background="Aqua" BorderBrush="Blue"
                 BorderThickness="5"  HorizontalAlignment="Center"
                 CornerRadius="5">
            <TextBlock>Click Here</TextBlock>
         </Border>
      </ControlTemplate>
      
   </Window.Resources>
   
   <StackPanel>      
      <Button FontWeight="Bold" Template="{StaticResource myButtonTemplate}"
              Click="myButton_Click" Name="myButton">
         Click Me
      </Button>
   </StackPanel>
</Window>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Windows;

namespace ButtonTemplate0
{
   /// <summary>
   /// Interaction logic for Window1.xaml
   /// </summary>
   public partial class Window1 : Window
   {
      public Window1()
      { InitializeComponent(); }

      private void myButton_Click( object sender, RoutedEventArgs e )
      {
         MessageBox.Show( "Button Clicked", "Button Message" );
      }
   }
}
This post is licensed under CC BY 4.0 by the author.