--- title: "Walkthrough: Create a Button by Using XAML" description: Use this walkthrough to learn how to create an animated button for use in a Windows Presentation Foundation application using XAML. ms.date: "03/30/2017" helpviewer_keywords: - "buttons [WPF]" ms.assetid: 138c41c4-1759-4bbf-8d77-77031a06a8a0 --- # Walkthrough: Create a Button by Using XAML The objective of this walkthrough is to learn how to create an animated button for use in a Windows Presentation Foundation (WPF) application. This walkthrough uses styles and a template to create a customized button resource that allows reuse of code and separation of button logic from the button declaration. This walkthrough is written entirely in [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)]. > [!IMPORTANT] > This walkthrough guides you through the steps for creating the application by typing or copying and pasting [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)] into Visual Studio. If you would prefer to learn how to use a designer to create the same application, see [Create a Button by Using Microsoft Expression Blend](walkthrough-create-a-button-by-using-microsoft-expression-blend.md). The following figure shows the finished buttons. ![Custom buttons that were created by using XAML](./media/custom-button-animatedbutton-5.gif "custom_button_AnimatedButton_5") ## Create Basic Buttons Let's start by creating a new project and adding a few buttons to the window. ### To create a new WPF project and add buttons to the window 1. Start Visual Studio. 2. **Create a new WPF project:** On the **File** menu, point to **New**, and then click **Project**. Find the **Windows Application (WPF)** template and name the project "AnimatedButton". This will create the skeleton for the application. 3. **Add basic default buttons:** All the files you need for this walkthrough are provided by the template. Open the Window1.xaml file by double clicking it in Solution Explorer. By default, there is a element in Window1.xaml. Remove the element and add a few buttons to the [!INCLUDE[TLA#tla_xaml](../../../includes/tlasharptla-xaml-md.md)] page by typing or copy and pasting the following highlighted code to Window1.xaml: ```xaml ``` Press F5 to run the application; you should see a set of buttons that looks like the following figure. ![Three basic buttons](./media/custom-button-animatedbutton-1.gif "custom_button_AnimatedButton_1") Now that you have created the basic buttons, you are finished working in the Window1.xaml file. The rest of the walkthrough focuses on the app.xaml file, defining styles and a template for the buttons. ## Set Basic Properties Next, let's set some properties on these buttons to control the button appearance and layout. Rather than setting properties on the buttons individually, you will use resources to define button properties for the entire application. Application resources are conceptually similar to external Cascading Style Sheets (CSS) for Web pages; however, resources are much more powerful than Cascading Style Sheets (CSS), as you will see by the end of this walkthrough. To learn more about resources, see [XAML Resources](/dotnet/desktop-wpf/fundamentals/xaml-resources-define). ### To use styles to set basic properties on the buttons 1. **Define an Application.Resources block:** Open app.xaml and add the following highlighted markup if it is not already there: ```xaml ``` Resource scope is determined by where you define the resource. Defining resources in `Application.Resources` in the app.xaml file enables the resource to be used from anywhere in the application. To learn more about defining the scope of your resources, see [XAML Resources](/dotnet/desktop-wpf/fundamentals/xaml-resources-define). 2. **Create a style and define basic property values with it:** Add the following markup to the `Application.Resources` block. This markup creates a that applies to all buttons in the application, setting the of the buttons to 90 and the to 10: ```xaml ``` The property specifies that the style applies to all objects of type . Each sets a different property value for the . Therefore, at this point every button in the application has a width of 90 and a margin of 10. If you press F5 to run the application, you see the following window. ![Buttons with a width of 90 and a margin of 10](./media/custom-button-animatedbutton-2.gif "custom_button_AnimatedButton_2") There is much more you can do with styles, including a variety of ways to fine-tune what objects are targeted, specifying complex property values, and even using styles as input for other styles. For more information, see [Styling and Templating](styles-templates-overview.md). 3. **Set a style property value to a resource:** Resources enable a simple way to reuse commonly defined objects and values. It is especially useful to define complex values using resources to make your code more modular. Add the following highlighted markup to app.xaml. ```xaml ``` Directly under the `Application.Resources` block, you created a resource called "GrayBlueGradientBrush". This resource defines a horizontal gradient. This resource can be used as a property value from anywhere in the application, including inside the button style setter for the property. Now, all the buttons have a property value of this gradient. Press F5 to run the application. It should look like the following. ![Buttons with a gradient background](./media/custom-button-animatedbutton-3.gif "custom_button_AnimatedButton_3") ## Create a Template That Defines the Look of the Button In this section, you create a template that customizes the appearance (presentation) of the button. The button presentation is made up of several objects including rectangles and other components to give the button a unique look. So far, the control of how buttons look in the application has been confined to changing properties of the button. What if you want to make more radical changes to the button's appearance? Templates enable powerful control over the presentation of an object. Because templates can be used within styles, you can apply a template to all objects that the style applies to (in this walkthrough, the button). ### To use the template to define the look of the button 1. **Set up the template:** Because controls like have a property, you can define the template property value just like the other property values we have set in a using a . Add the following highlighted markup to your button style. ```xaml ``` 2. **Alter button presentation:** At this point, you need to define the template. Add the following highlighted markup. This markup specifies two elements with rounded edges, followed by a . The is used to host the of the button. A displays the content of the button. In this walkthrough, the content is text ("Button 1", "Button 2", "Button 3"). All of the template components (the rectangles and the ) are laid out inside of a . ```xaml ``` Press F5 to run the application. It should look like the following. ![Window with 3 buttons](./media/custom-button-animatedbutton-4.gif) 3. **Add a glasseffect to the template:** Next you will add the glass. First you create some resources that create a glass gradient effect. Add these gradient resources anywhere within the `Application.Resources` block: ```xaml ``` These resources are used as the for a rectangle that we insert into the of the button template. Add the following highlighted markup to the template. ```xaml ``` Notice that the of the rectangle with the `x:Name` property of "glassCube" is 0, so when you run the sample, you do not see the glass rectangle overlaid on top. This is because we will later add triggers to the template for when the user interacts with the button. However, you can see what the button looks like now by changing the value to 1 and running the application. See the following figure. Before proceeding to the next step, change the back to 0. ![Custom buttons that were created by using XAML](./media/custom-button-animatedbutton-5.gif "custom_button_AnimatedButton_5") ## Create Button Interactivity In this section, you will create property triggers and event triggers to change property values and run animations in response to user actions such as moving the mouse pointer over the button and clicking. An easy way to add interactivity (mouse-over, mouse-leave, click, and so on) is to define triggers within your template or style. To create a , you define a property "condition" such as: The button property value is equal to `true`. You then define setters (actions) that take place when the trigger condition is true. ### To create button interactivity 1. **Add template triggers:** Add the highlighted markup to your template. ```xaml ``` 2. **Add property triggers:** Add the highlighted markup to the `ControlTemplate.Triggers` block: ```xaml ``` Press F5 to run the application and see the effect as you run the mouse pointer over the buttons. 3. **Add a focus trigger:** Next, we'll add some similar setters to handle the case when the button has focus (for example, after the user clicks it). ```xaml ``` Press F5 to run the application and click on one of the buttons. Notice that the button stays highlighted after you click it because it still has focus. If you click another button, the new button gains focus while the last one loses it. 4. **Add animations for** **and** **:** Next we add some animations to the triggers. Add the following markup anywhere inside of the `ControlTemplate.Triggers` block. ```xaml ``` The glass rectangle shrinks when the mouse pointer moves over the button and returns back to normal size when the pointer leaves. There are two animations that are triggered when the pointer goes over the button ( event is raised). These animations shrink the glass rectangle along the X and Y axis. Notice the properties on the elements — and . The specifies that the animation occurs over half a second, and specifies that the glass shrinks by 10%. The second event trigger () simply stops the first one. When you stop a , all the animated properties return to their default values. Therefore, when the user moves the pointer off the button, the button goes back to the way it was before the mouse pointer moved over the button. For more information about animations, see [Animation Overview](../graphics-multimedia/animation-overview.md). 5. **Add an animation for when the button is clicked:** The final step is to add a trigger for when the user clicks the button. Add the following markup anywhere inside of the `ControlTemplate.Triggers` block: ```xaml ``` Press F5 to run the application, and click one of the buttons. When you click a button, the glass rectangle spins around. ## Summary In this walkthrough, you performed the following exercises: - Targeted a to an object type (). - Controlled basic properties of the buttons in the entire application using the . - Created resources like gradients to use for property values of the setters. - Customized the look of buttons in the entire application by applying a template to the buttons. - Customized behavior for the buttons in response to user actions (such as , , and ) that included animation effects. ## See also - [Create a Button by Using Microsoft Expression Blend](walkthrough-create-a-button-by-using-microsoft-expression-blend.md) - [Styling and Templating](styles-templates-overview.md) - [Animation Overview](../graphics-multimedia/animation-overview.md) - [Painting with Solid Colors and Gradients Overview](../graphics-multimedia/painting-with-solid-colors-and-gradients-overview.md) - [Bitmap Effects Overview](../graphics-multimedia/bitmap-effects-overview.md)