1.2.15 Basic Event Handlers for Buttons

Video: Creating Button Controls (8:08)
A Button Can Trigger C# Code

A WPF application can contain a Button.

When the user activates the button, WPF raises a Click event.

The application can connect that event to an event-handler method.

At this stage, treat the event handler as:

Detailed method design begins in the next module.

XAML Can Connect a Button to a Handler

A simplified example is:

Activity diagram showing button Click, then ShowPlayer_Click, then C# statements run.
ButtonClick.
  • New
  • Changed
  • Removed
Keep the Event Handler Small

An introductory handler should perform the focused action required by the current application.

Avoid adding complex logic or redesigning the project.

Later method instruction will help you move behavior into focused methods when appropriate.

Event-Driven Flow Is Different from a Straight Startup Sequence

Some C# runs because the application starts.

An event handler waits until an event occurs.

Conceptually:

Activity diagram showing application running, then user clicks button, then Click event, then handler executes.
ApplicationRunning.
  • New
  • Changed
  • Removed

That is the basic WPF interaction you need at this point.