#298 – Button Basics – Content and Click

The Button control in WPF represents a visual representation of a button that a user can click on.  It will typically initate some action.

Here’s an example, with two buttons.

You specify a button’s content–typically a text label–using the Content property.  Like other controls that descend from FrameworkElement, you can specify the height and width using the Height and Width properties.

		<Button Content="Save" Height="25" Width="80" />

You can execute some piece of code when the user clicks on the button by adding an event handler for the button’s Click event.

First, attach the handler in the XAML:

		<Button Content="Save" Height="25" Width="80" Click="Save_Click"/>

Then, define the corresponding method in the code-behind class:

        private void Save_Click(object sender, RoutedEventArgs e)
            MessageBox.Show("You clicked on the Save button");