liveBook · Manning

EventToCommandBehavior

As explained in .NET MAUI in Action

EventToCommandBehavior is a .NET MAUI behavior that invokes a command when a specified UI event occurs and can pass event-derived parameters.

EventToCommandBehavior extends a .NET MAUI control without subclassing it, invoking a command when a specified UI event occurs and passing a binding-derived parameter. section 9.2 section 9.7

Complete a to-do item from a CheckBox

Install the CommunityToolkit.Maui NuGet package, initialize the toolkit in MauiProgram.cs, and import CommunityToolkit.Maui.Behaviors into MainPage.xaml. Add EventToCommandBehavior to the CheckBox, set EventName to CheckedChanged, bind Command to the page's CompleteTodoCommand, and use CommandParameter="{Binding .}" to pass the current to-do item to the database update method. section 9.2

Use the behavior when:

  • A View must respond to a control event through ViewModel functionality. section 9.7
  • The command needs the whole bound item or another binding-derived value rather than a bespoke event handler. section 9.2

How .NET MAUI in Action covers it

How deep it goes
Provides installation and XAML configuration, but does not detail the behavior's internal implementation.
Examples
Uses a CheckBox CheckedChanged event to pass a bound to-do item to CompleteTodoCommand.
What it assumes
Assumes familiarity with .NET MAUI, XAML bindings, commands, and ViewModels.
What you can do after
Apply it via CommunityToolkit.Maui to invoke ViewModel commands from XAML events with parameters.
How it connects
Connects behaviors, commands, data binding, ViewModels, and the .NET MAUI Community Toolkit.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.