XAML Button

Introduction to WPF Button using C# and XAML

The Button control is one of the basic controls in WPF. A button is used to click and execute code on its click event handler. A button control can be represented at design-time using the XAML <Button> element. The Button class in C# represents the WPF button at run-time. This tutorial and code examples demonstrate how to create a button control, add a button control event handler, format a button control, and display an image in a button control using C# and XAML.
 
Creating a Button

The Button XAML element represents a WPF Button control.
  1. <Button/>  
The Width and Height attributes of the Button element represent the width and the height of a Button. The Content property of the Button element sets the text of a button. The x:Name attribute represents the name of the control, that is a unique identifier of a control. You may also use the Name property to set the name of a control.

The code snippet in Listing 1 creates a Button control and sets the name, height, width and content of a Button control.
  1. <Button x:Name="DrawCircleButton" Height="40" Width="120"   
  2.    Content="Draw Circle" >  
  3. </Button>  
Listing 1

The default property of a button is Content. The following code snippet creates the same button as created by Listing 1.
  1. <Button x:Name="DrawCircleButton" Height="40" Width="120" >  
Draw Circle
  1. </Button>  
The output looks like Figure 1.



Figure 1

Positioning a Button

If you place a button on a Grid control, the button will be placed in the center of the Grid. If you are using a Canvas as your parent control of the Button, the Button will be placed in the left top corner of the Canvas.

You can use the Margin property to position a button control. If you are using a Canvas, you may also use Canvas.Left and Canvas.Top properties.

You may also use the VerticalAlignment and HorizontalAlignment attributes to set vertical alignment and horizontal alignment of a button.

The code snippet in Listing 2 sets the position of the Button control in the top-left corner of the page.
  1. <Button x:Name="DrawCircleButton" Height="40" Width="120"   
  2.    Canvas.Left="10" Canvas.Top="10"   
  3.    Content="Draw Circle"  
  4.    VerticalAlignment="Top"   
  5.    HorizontalAlignment="Left">  
  6. </Button>  
Listing 2

Adding a Button Click Event Handler

The Click attribute of the Button element adds the click event handler. The following code adds the click event handler for a Button.
  1. <Button x:Name="DrawCircleButton" Height="40" Width="120"   
  2.    Canvas.Left="10" Canvas.Top="10"   
  3.    Content="Draw Circle"  
  4.    VerticalAlignment="Top"   
  5.    HorizontalAlignment="Left">  
  6.    Click="DrawCircleButton_Click"  
  7. </Button>  
The code for the click event handler looks like the following.
  1. private void DrawCircleButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. }  
Now, whatever code you write in the click event handler will be executed on the Button click. The code listed in Listing 3 creates a circle on the Button click event handler.
  1. private void DrawCircleButton_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    // creates a Circle  
  4.    Ellipse circle = new Ellipse();  
  5.    circle.Width = 200;  
  6.    circle.Height = 200;  
  7.    circle.Fill = new SolidColorBrush(Colors.Yellow);  
  8.    circle.Stroke = new SolidColorBrush(Colors.Black);  
  9.    circle.StrokeThickness = 4;  
  10.    LayoutRoot.Children.Add(circle);  
  11. }  
Listing 3

Formatting a Button


Not let's get a little creative. How about we create a Button control with a border formatting, background and foreground of the Button?

The BorderBrush property of the Button sets a brush to draw the border of a Button. You may use any brush to fill the border. The following code snippet uses a linear gradient brush to draw the border with a combination of red and blue color.
  1. <Button.BorderBrush>  
  2.    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  3.       <GradientStop Color="Blue" Offset="0" />  
  4.       <GradientStop Color="Red" Offset="1.0" />  
  5.    </LinearGradientBrush>  
  6. </Button.BorderBrush>  
The Background and Foreground properties of the Button set the background and foreground colors of a Button. You may use any brush to fill the border. The following code snippet uses linear gradient brushes to draw the background and foreground of a Button.
  1. <Button.Background>  
  2.    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  3.       <GradientStop Color="Blue" Offset="0.1" />  
  4.       <GradientStop Color="Orange" Offset="0.25" />   
  5.       <GradientStop Color="Green" Offset="0.75" />  
  6.       <GradientStop Color="Red" Offset="1.0" />  
  7.    </LinearGradientBrush>  
  8. </Button.Background>  
  9. <Button.Foreground>  
  10.    <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >   
  11.       <GradientStop Color="Orange" Offset="0.25" />  
  12.       <GradientStop Color="Green" Offset="1.0" />   
  13.    </LinearGradientBrush>  
  14. </Button.Foreground>  
The final complete source code looks like this.
  1. <Window x:Class="ButtonSample.Window1"  
  2.    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.    Title="Window1" Height="300" Width="300">  
  5.    <Canvas Name="LayoutRoot">  
  6.       <Button x:Name="DrawCircleButton" Height="40" Width="120"   
  7.          Canvas.Left="10" Canvas.Top="10" Click="DrawCircleButton_Click" >  
  8.   
  9.          <Button.BorderBrush>  
  10.             <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  11.                <GradientStop Color="Blue" Offset="0" />  
  12.                <GradientStop Color="Red" Offset="1.0" />  
  13.             </LinearGradientBrush>  
  14.          </Button.BorderBrush>  
  15.       <Button.Background>  
  16.          <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  17.             <GradientStop Color="Blue" Offset="0.1" />  
  18.             <GradientStop Color="Orange" Offset="0.25" />  
  19.             <GradientStop Color="White" Offset="0.75" />  
  20.             <GradientStop Color="Red" Offset="1.0" />  
  21.          </LinearGradientBrush>  
  22.       </Button.Background>  
  23.       <Button.Foreground>  
  24.          <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  25.             <GradientStop Color="Black" Offset="0.25" />  
  26.             <GradientStop Color="Green" Offset="1.0" />  
  27.          </LinearGradientBrush>  
  28.       </Button.Foreground>  
  29.       Draw Circle  
  30.       </Button>  
  31.    </Canvas>  
  32. </Window>  
The new Button looks as iin Figure 2.



Figure 2

Mouse Rollover Formatting


How about giving some affects to your Button control when a mouse is over the Button and the mouse leaves the Button area? We can do this by adding MouseEnter and MouseLeave event handlers.

The following code snippet adds Mouse Enter and Mouse Leave event handlers.
  1. <Button x:Name="DrawCircleButton" Height="40" Width="120"   
  2.    Canvas.Left="10" Canvas.Top="10" Click="DrawCircleButton_Click"  
  3.    MouseEnter="DrawCircleButton_MouseEnter" MouseLeave="DrawCircleButton_MouseLeave">  
The code listed in Listing 4 sets the background and foreground colors of a Button on mouse enter and mouse leave event handlers.
  1. private void DrawCircleButton_MouseEnter(object sender, MouseEventArgs e)  
  2. {  
  3.    DrawCircleButton.Background = new SolidColorBrush(Colors.Yellow );  
  4.    DrawCircleButton.Foreground = new SolidColorBrush(Colors.Green);  
  5. }  
  6.   
  7. private void DrawCircleButton_MouseLeave(object sender, MouseEventArgs e)  
  8. {  
  9.    DrawCircleButton.Background = new SolidColorBrush(Colors.Red);  
  10.    DrawCircleButton.Foreground = new SolidColorBrush(Colors.Purple);  
  11. }  
Listing 4

Setting Image as Background of a Button


To set an image as the background of a Button, we can set an image as the Background of the Button. The following code snippet sets the background of a Button to an image.
  1. <Button.Background>  
  2.    <ImageBrush ImageSource="dock.jpg" />  
  3. </Button.Background>  
The new output looks as in Figure 3.



Figure 3

Creating a Button Dynamically


The code listed in Listing 5 creates a Button control programmatically. First, it creates a Button object and sets its width, height, contents, background and foreground and later the Button is added to the LayoutRoot.
  1. private void CreateAButton()  
  2. {  
  3.    Button btn = new Button();  
  4.    btn.Height = 80;  
  5.    btn.Width = 150;  
  6.    btn.Content = "Click ME";  
  7.    btn.Background = new SolidColorBrush(Colors.Orange);  
  8.    btn.Foreground = new SolidColorBrush(Colors.Black);  
  9.    LayoutRoot.Children.Add(btn);  
  10. }  
Listing 5

Summary


In this article, I discussed how to create a Button control using XAML. We also saw how to format a Button by setting its border, background and foreground properties. Then, we showed how to set an image as the background of a Button. In the end of this article, we saw how to create a Button dynamically.


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.