[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]View Models
  [Expand]Commands
  [Collapse]Behaviors
    Getting Started
   [Collapse]Predefined Set
     EventToCommand
     KeyToCommand
     FocusBehavior
     ValidationErrorsHostBehavior
     ConfirmationBehavior
     DependencyPropertyBehavior
     EnumItemsSourceBehavior
     CompositeCommandBehavior
     FunctionBindingBehavior
     MethodToCommandBehavior
     CurrentWindowSerializationBehavior
     BarSubItemThemeSelectorBehavior
     BarSplitItemThemeSelectorBehavior
     RibbonGalleryThemeSelectorBehavior
     RibbonGalleryItemThemePaletteSelectorBehavior
     ReadOnlyDependencyPropertyBindingBehavior
    How to: Create a Custom Behavior
  [Expand]Services
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

EventToCommand

The EventToCommand class is a Behavior that allows you to bind an event to a command. This way, the bound command is invoked like an event handler when the event is raised.

This topic describes how to use the EventToCommand behavior.

Expanded Getting Started With EventToCommand

Assume that there is a ListBox control that displays data. When an end user clicks an item in the ListBox, it is necessary to show an edit form.

To accomplish this task, you can use the EventToCommand behavior. Place the EventToCommand into the Interaction.Behaviors collection for the ListBox control and customize it as shown below.

This code defines an EventToCommand that processes the MouseDoubleClick event for the ListBox. When the event is raised, the EventToCommand invokes the bound EditCommand. This command requires a parameter: a Person object to be edited.

The code below shows the EditCommand implementation code both in a POCO ViewModel and a common ViewModel.

In this case, it is necessary to take the clicked ListBoxItem and obtain its DataContext - this is what the EventToCommand should pass to the EditCommand as a parameter. This operation is performed by the custom ListBoxEventArgsConverter. Its code is shown below.

The ListBoxEventArgsConverter class implements the IEventArgsConverter interface. This interface provides the Convert method, which is used by the EventToCommand for conversion event arguments.

In this scenario, the EventToCommand passes a MouseEventArgs object to the ListBoxEventArgsConverter. The converter finds the clicked ListBoxItem using the LayoutTreeHelper class and returns its DataContext, which contains an underlying Person object. The resulting Person object is then passed to the bound EditCommand.

Expanded Specifying an Event

The EventToCommand behavior allows you to specify an event using any of the following properties:

EventName is useful when a source object provides an event.

Unlike EventName, the Event property is of the System.Windows.RoutedEvent type and can be used to specify attached events. For example:

Expanded Specifying a Source Object

The source object is an object that provides the event for processing by the EventToCommand. By default, the EventToCommand processes the defined event for the associated control. This scenario is shown below.

If it's necessary, you can manually specify the source object for the EventToCommand. To do this, bind the EventTriggerBase<T>.SourceObject property, or specify the object's name using the EventTriggerBase<T>.SourceName property.

Expanded Passing a Parameter to the Bound Command

You can provide a parameter to the bound command using the EventToCommandBase.CommandParameter property.

Alternatively, you can pass the event's arguments to the command as a parameter by setting the EventToCommand.PassEventArgsToCommand property to true.

If you have a clean MVVM architecture, you may not want to pass event arguments to View Models. In this case, you can convert the event arguments to an object suitable for the command. To do this, specify a converter using the EventToCommand.EventArgsConverter property.

The defined EventArgsConverter should implement the IEventArgsConverter interface. You can also derive the converter from the EventArgsConverterBase<TArgs> class, which already implements the IEventArgsConverter interface.

When implementing the converter, you can use the LayoutTreeHelper class that provides useful functions for searching nodes in the visual tree. For instance:

Expanded Specifying Modifier Keys as an Additional Condition of the Command Execution

The EventToCommand behavior allows you to invoke a command only when modifier keys are pressed. Use the EventToCommand.ModifierKeys property to specify modifier keys.

In the example above, the bound EditCommand is invoked when an end user clicks a ListBoxItem while pressing the Ctrl and Alt keys.

Expanded Marking Routed Events as Handled

Set the EventToCommandBase.MarkRoutedEventsAsHandled property to True to mark routed events as handled when the bound command is executed.

In this case, the EventToCommand sets the e.Handled parameter of corresponding event arguments to True right after the bound command is executed.

Expanded Using the Dispatcher to Invoke the bound Command

The EventToCommand class provides the EventToCommandBase.DispatcherPriority property. You can set this property to use the Dispatcher to invoke the bound command.

Expanded Disabling a Control When a Command Cannot be Executed

The EventToCommand class provides the EventToCommand.AllowChangingEventOwnerIsEnabled property, which is False by default. If you set this property to True, the EventToCommand disables (sets the IsEnabled property to False) the associated control when the bound command cannot be executed (when the ICommand.CanExecute method returns False).

Expanded Processing Events of a Disabled Control

The EventToCommand class provides the EventToCommandBase.ProcessEventsFromDisabledEventOwner property, which is True by default. This means that the EventToCommand processes events for the associated control even if the associated control is disabled. If you need to prevent this behavior, set the ProcessEventsFromDisabledEventOwner property to False.

Expanded Example

Note

A complete sample project is available at https://github.com/DevExpress-Examples/how-to-use-eventtocommand-t142075.

Is this topic helpful?​​​​​​​