TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to WPF

WPF Button Control Example

Use the WPF Button control, adding C# code to handle Button events.
Button, WPF. With a Button, we provide a visual indication of our program's functionality. When the user clicks on a button, something happens. We use an event handler in C# when the click occurs.
To start, please create a new C# WPF project. In the designer view of your window, add a Button control. Locate the Button control in the Toolbox panel. You will see that 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.

Event
In the event handler (by default named Button_Click) we can execute any C# statements. In this example, we set the Title of the Window (referenced by "this") to a new string value. This string appears after the button is clicked.
Example markup: XAML <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> Example code: C# 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"; } } }
Padding. A Button can have padding. With Padding, we specify values to add space inside the control. With one Padding value, all sides are spaced evenly. With four 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.

Example markup, Padding: XAML <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>
Attributes. A Button can have many different attributes. The Padding attribute, shown above, is one. And the Click attribute allows you to specify an event handler for the Clicked event. Other attributes also are available.

HorizontalAlignment: With the HorizontalAlignment and VerticalAlignment properties, we can anchor, center, or stretch Buttons.

IsEnabled: We use the IsEnabled property to disable a button. This makes it unusable.

IsEnabled

ToolTip: A Button can have a ToolTip. The ToolTip helps indicate what a control (like a Button) does.

ToolTip
Summary. We created a Button control in a WPF (Windows Presentation Foundation) program. We linked this Button to an event handler. Our program modifies its state (its Window Title) whenever a specific action (a click) occurs.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf