Home
WPF
Button Control Example
Updated Jun 28, 2021
Dot Net Perls
Button, WPF. With a Button, we provide a visual indication of our program's functionality. When the user clicks on a button, something happens.
Notes, event handler. We use an event handler in C# when the click occurs. This is the Button_Click event handler. Properties, like IsEnabled are also useful.
Event handler. In Button_Click we can execute C# code. Here we set the Title of the Window (referenced by "this") to a new string value. This string appears after the button is clicked.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> </Grid> </Window>
using System.Windows; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { this.Title = "Clicked"; } } }
Getting started. In the designer view of your WPF window, add a Button. Locate the Button in the Toolbox panel. We find the XAML markup changes and a Button element is inserted.
Next We want to make the Button do something when the user clicks on it. We must modify the XAML markup.
So Near the end of the Button element, add the Click attribute. Visual Studio will offer the option to make a new event handler.
ToolTip
IsEnabled
Padding: Adds whitespace inside the button Click: Allows you to specify an event handler for the Clicked event HorizontalAlignment, VerticalAlignment: With these properties, we can anchor, center, or stretch Buttons IsEnabled: We use the IsEnabled property to disable a button This makes it unusable ToolTip: A Button can have a ToolTip The ToolTip helps indicate what a control (like a Button) does
Padding. This adds space inside the control. With 1 Padding value, all sides are spaced evenly. With 4 values, the values are used for the left, top, right and bottom.
Here We apply four different padding values to a button. Each side of the button (in the screenshot) has a different padding.
<Window x:Class="WpfApplication15.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Button" HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Padding="10,20,30,40"/> </Grid> </Window>
A summary. We created a Button in a WPF program. We linked this Button to an event handler. Our program modifies its state (its Window Title) whenever a specific action (a click) occurs.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 28, 2021 (edit).
Home
Changes
© 2007-2025 Sam Allen