1.2.14 Using WPF Buttons

Video: Introduction to WPF (4:58)
A Button Is a WPF User-Interface Control

A WPF Button gives the user a visible action they can activate.

A simple XAML button can look like:

Activity diagram showing XAML defines the control, then C# handles the Click event.
XAMLDefinesTheControl.
  • New
  • Changed
  • Removed
Content Controls What the User Sees

For:

Content="Show Roster"

the button communicates its action to the user.

Use concise, meaningful wording.

The label should help the user predict what activating the button will do.

Click Connects the Control to C# Behavior

For:

Click="ShowRoster_Click"

WPF uses the named event handler when the user activates the button.

The handler lives in the code-behind for the supplied window or control.

Preserve the Supplied Project Structure

When the course provides a WPF project:

  • use the existing window;
  • use existing controls when directed;
  • preserve existing names unless the task requires a change;
  • do not create a second window merely to avoid understanding the supplied one.
Buttons Are Objects Too

A WPF Button is represented by an object at runtime.

You do not need to study the Button class hierarchy yet.

The useful idea is that user-interface controls are software objects with members and events.

Keep the Current Goal Small

At this stage, use buttons to connect a user action to a small C# event handler.

Complex interface architecture and advanced event behavior belong later.