[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
 [Expand]What's Installed
 [Expand]Common Concepts
 [Collapse]MVVM Framework
    Getting Started
   [Collapse]Predefined Set
    How to: Create a Custom Behavior
   Data Annotation Attributes
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)


The EventToCommand class is a special 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 documentation topic describes how to use the EventToCommand and the features it provides.

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 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 (specified by the EventToCommand.EventName property) 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

There are times when it is necessary to invoke a command only when certain modifier keys are pressed. This additional condition of command execution can be set using the EventToCommand.ModifierKeys property.

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

Expanded Marking routed events as handled

When you are required to process a routed event with EventToCommand, you may want to mark the routed event as handled when the bound command is executed. To enable this behavior, set the EventToCommandBase.MarkRoutedEventsAsHandled property to True.

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 lass 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


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

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