Custom Shaped Button


   
  
<Window x:Class="ControlTemplates.SimpleShapedButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Shaped Button" Height="510" Width="600">
  <Window.Resources>
    <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}" >      
        <Grid>          
          <Path Name="Border" Stroke="Red" StrokeThickness="2" Stretch="Fill">
            <Path.Fill>            
                <RadialGradientBrush RadiusX="1" RadiusY="1" GradientOrigin="0.7,0.3" >
                  <GradientStop Color="Red" Offset="0" />
                  <GradientStop Color="Blue" Offset="1" />
                </RadialGradientBrush>            
            </Path.Fill>
            <Path.Data>
              <CombinedGeometry GeometryCombineMode="Union">
                <CombinedGeometry.Geometry1>                  
                  <EllipseGeometry Center="70 20" RadiusX="50" RadiusY="25"></EllipseGeometry>
                </CombinedGeometry.Geometry1>
                <CombinedGeometry.Geometry2>
                  <RectangleGeometry Rect="100 0 150 30"></RectangleGeometry>                  
                </CombinedGeometry.Geometry2>                
              </CombinedGeometry>
            </Path.Data>
          </Path>
        <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2" SnapsToDevicePixels="True" ></Rectangle>
          <Border Margin="5,10,5,10">
            <ContentPresenter  Name="Content" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" TextBlock.Foreground="White"></ContentPresenter>
          </Border>
        </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter TargetName="Border" Property="Fill" Value="DarkRed" />
        </Trigger>                
        <Trigger Property="IsPressed" Value="True">
          <Setter TargetName="Border" Property="Fill" Value="IndianRed" />
          <Setter TargetName="Border" Property="Stroke" Value="DarkKhaki" />
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="True">
          <Setter TargetName="FocusCue" Property="Visibility" Value="Visible"></Setter>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
          <Setter TargetName="Content" Property="TextBlock.Foreground" Value="Gray"></Setter>
          <Setter TargetName="Border" Property="Fill" Value="MistyRose"></Setter>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Window.Resources>
  <StackPanel Width="150" HorizontalAlignment="Left">
    <Button Template="{StaticResource ButtonTemplate}" Click="Clicked" Name="cmdOne">A Simple Button
    </Button>
     <Button Template="{StaticResource ButtonTemplate}" IsEnabled="False" Click="Clicked" Name="cmdFour">
      <StackPanel>
        <Label>sfsdfsf</Label>
      </StackPanel>
      
    </Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ControlTemplates
{
    public partial class SimpleShapedButton : System.Windows.Window
    {
        public SimpleShapedButton()
        {
            InitializeComponent();
        }
        private void Clicked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("You clicked " + ((Button)sender).Name);
        }
    }
}

   
    
     


Simple Custom Button


   
  
<Window x:Class="ControlTemplates.SimpleCustomButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SimpleCustomButton" Height="410" Width="400">
  <Window.Resources>
    <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
      <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2"
       Background="Red" TextBlock.Foreground="White" Name="Border">
        <Grid>
          <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black"
           StrokeThickness="1" StrokeDashArray="1 2"
           SnapsToDevicePixels="True" ></Rectangle>
          <ContentPresenter RecognizesAccessKey="True"
         Margin="{TemplateBinding Padding}"></ContentPresenter>
        </Grid>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter TargetName="Border" Property="Background" Value="Red" />
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
          <Setter TargetName="Border" Property="Background" Value="IndianRed" />
          <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
        </Trigger>
        <Trigger Property="IsKeyboardFocused" Value="True">
          <Setter TargetName="FocusCue" Property="Visibility" Value="Visible" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
          <Setter TargetName="Border" Property="TextBlock.Foreground" Value="Gray" />
          <Setter TargetName="Border" Property="Background" Value="MistyRose" />
        </Trigger>

      </ControlTemplate.Triggers>
    </ControlTemplate>

  </Window.Resources>
  <StackPanel Margin="10">
    <Button Template="{StaticResource ButtonTemplate}" Click="Clicked" Name="cmdOne">
      A Simple Button with a Custom Template
    </Button>
    <Button Template="{StaticResource ButtonTemplate}" IsEnabled="False" Click="Clicked" Name="cmdFour">
      A Disabled Button
    </Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ControlTemplates
{

    public partial class SimpleCustomButton : System.Windows.Window
    {

        public SimpleCustomButton()
        {
            InitializeComponent();
        }

        private void Clicked(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("You clicked " + ((Button)sender).Name);
        }

    }
}

   
    
     


Button mouse down preview


   
  


<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Routed Events" Height="400" Width="800">
  
    <Grid Name="contentGrid" Background="Red">
        <Rectangle Name="clickMeRectangle" 
                   Height="70" 
                   Width="70" 
                   Stroke="Black" 
                   Fill="CadetBlue" />
        <Button Name="clickMeButton" 
                Height="23" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Top" 
                Width="70" 
                PreviewMouseDown="Generic_MouseDown" 
                Click="clickMeButton_Click">Click Me</Button>
        <TextBlock Name="outputText" />
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private void Generic_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(outputText.Text);
            Console.WriteLine(e.RoutedEvent.Name);
            Console.WriteLine(sender.ToString());
            Console.WriteLine(((FrameworkElement)e.Source).Name);
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            outputText.Text = outputText.Text;
        }

        private void clickMeButton_Click(object sender, RoutedEventArgs e)
        {
            outputText.Text = "Button clicked:" + outputText.Text;
        }
    }
}

   
    
     


Button mouse down event


   
  


<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Routed Events" Height="400" Width="800">
  
    <Grid Name="contentGrid" Background="Red">
        <Rectangle Name="clickMeRectangle" 
                   Height="70" 
                   Width="70" 
                   Stroke="Black" 
                   Fill="CadetBlue" />
        <Button Name="clickMeButton" 
                Height="23" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Top" 
                Width="70" 
                MouseDown="Generic_MouseDown" 
                PreviewMouseDown="Generic_MouseDown" 
                Click="clickMeButton_Click">Click Me</Button>
        <TextBlock Name="outputText" />
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private void Generic_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(outputText.Text);
            Console.WriteLine(e.RoutedEvent.Name);
            Console.WriteLine(sender.ToString());
            Console.WriteLine(((FrameworkElement)e.Source).Name);
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            outputText.Text = outputText.Text;
        }

        private void clickMeButton_Click(object sender, RoutedEventArgs e)
        {
            outputText.Text = "Button clicked:" + outputText.Text;
        }
    }
}

   
    
     


To add a button control and a text block to the canvas

   
           

<Window x:Class="LineInCustomSystem"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Line In Custom System" Height="240" Width="220">
  <Border BorderBrush="Black" BorderThickness="1" Height="200"
    Width="200">
    <Canvas Height="200" Width="200">
            <Button Canvas.Top="50" Canvas.Left="80" FontSize="15" Foreground="Red"
              Name="label1" Content="My Button" />
            <TextBlock Canvas.Top="120" Canvas.Left="20" FontSize="12pt"
              Foreground="Blue">
              <Bold>My Text Block</Bold>
            </TextBlock>
    </Canvas>
  </Border>
</Window>

   
    
    
    
    
    
    
    
    
    
    
     


The implementation of our button's Click event handler in Xaml


   
           

<Window x:Class="SimpleXamlApp.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="My Xaml App" Height="200" Width="300" 
  WindowStartupLocation ="CenterScreen">

  <Button Width="133" Height="24" Name="btnExitApp" Click ="btnExitApp_Clicked">
    Exit Application
  </Button>

  <x:Code>
  private void btnExitApp_Clicked(object sender, RoutedEventArgs e)
  {
    Application.Current.Shutdown();
  }
  </x:Code>
</Window>

   
    
    
    
    
    
    
    
    
    
    
     


Button with Inline Property Trigger


   
       


<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Button MinWidth="75" Margin="10" FontSize="50">
  <Button.Style>
    <Style TargetType="{x:Type Button}">
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Foreground" Value="Blue"/>
      </Trigger>
    </Style.Triggers>
    </Style>
  </Button.Style>
    OK
  </Button>
</Page>