Introduction

WPF is a presentation system for building Windows based applications having an attractive User Interface (UI). This is not only in Windows-based or standalone applications, this also works in browser-based applications. WPF is an engine that is a vector-based rendering engine that uses modern graphic hardware. WPF has many features and those features make WPF popular. Some of the feature are the following:

In this article we will learn about animated buttons using WPF. We will use style and a template to create a customized button. This customized button design part is written in the Extensible Application Markup Language (XAML). We will be learning step-by-step.

Create a Basic Button in WPF

step 1 :
Open the Visual Studio

Step 2 :
Select a new WPF project. Click the file menu and select New then click on the project. Find the Windows Desktop WPF Application template.

NewProject

Step 3
: Change the default Grid element into StackPanel and use the default buttons inside the StackPanel, as in the following code:

  1. <Window x:Class="WpfApplication1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="300" Width="300" Background="Black">
  5. <StackPanel HorizontalAlignment="Left">
  6. <Button>Button 1</Button>
  7. <Button>Button 2</Button>
  8. <Button>Button 3</Button>
  9. <Button>Button 3</Button>
  10. </StackPanel>
  11. </Window>

Output:

outputScreen1

Set Basic Properties

We will set the basic properties for the Button. We will use an Application resource to set the property. It is similar to Cascading Style Sheets (CSS) in any web pages. However application resources are much more powerful than CSS. Let's try to implement that.

OutputScreen3

Using Template Defines the Look of the Button

We will be using a template for the button. Templates generally give us a different look. There are various templates to make our button a unique look. Always remember we need to use a template inside the style tag.

Create Button Interactivity

Interactivity means mouse-over, mouse-leave, click and so on. For setting the button interactivity we need to use a property trigger and an event trigger. To define the trigger within our template or style, we need to set the property "condition", such as the IsMouseOver equal to true. The procedure to create the button interactivity is as the following.

Summary

In this article we learned various styles for a button, set the basic properties for the button that the entire application will use. We have also learned how to create resources like gardiants to be used for property values of the style setters and we have customized the behavior of a button in response to user actions, like mouseEnter, MouseLeave and click that all have animated effects.