Skip to main content

EventToCommand Class

Executes a command in response to a raised event.

Namespace: DevExpress.Mvvm.UI

Assembly: DevExpress.Xpf.Core.v23.2.dll

NuGet Package: DevExpress.Wpf.Core

Declaration

public class EventToCommand :
    EventToCommandBase

Remarks

EventToCommand: Basic Setup Example

Review the following example for information about the EventToCommand:

View Example: How to: Use EventToCommand

Specify a Source Object

A source object is an object that raises events. The default source object is a control associated with the EventToCommand behavior.

<UserControl>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="Loaded" Command="{Binding InitializeCommand}"/>
    </dxmvvm:Interaction.Behaviors>
</UserControl>

You can specify a different source object for the EventToCommand:

SourceObject
Allows you to bind the behavior’s source object to a control.
SourceName
Specifies the name of the behavior’s source object.
<UserControl>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand SourceName="list" EventName="MouseDoubleClick" Command="{Binding InitializeCommand}"/>
        <dxmvvm:EventToCommand SourceObject="{Binding ElementName=list}"
                               EventName="MouseDoubleClick" 
                               Command="{Binding InitializeCommand}"/>
    </dxmvvm:Interaction.Behaviors>
    <ListBox x:Name="list"/>
</UserControl>

Specify an Event

You can use one of the following properties to specify an event:

EventName

If the source object contains the event, use this property to specify the event’s name.

<dxe:TextEdit>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand Command="{Binding LoadedCommand}" EventName ="Loaded"/>
    </dxmvvm:Interaction.Behaviors>
</dxe:TextEdit>
Event

This property is of the RoutedEvent type. Use it to specify a routed event.

<dxe:TextEdit>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand Command="{Binding LoadedCommand}" Event="FrameworkElement.Loaded"/>
        <dxmvvm:EventToCommand Command="{Binding TextChangedCommand}" Event="TextBoxBase.TextChanged"/>
    </dxmvvm:Interaction.Behaviors>
</dxe:TextEdit>

Pass a Parameter to the Bound Command

Use the CommandParameter property to pass a parameter to a command:

<ListBox x:Name="list">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="MouseDoubleClick" 
                               Command="{Binding EditCommand}" 
                               CommandParameter="{Binding ElementName=list, Path=SelectedItem}"/>
    </dxmvvm:Interaction.Behaviors>
</ListBox>

Use one of the following properties to pass an event’s arguments to a command:

PassEventArgsToCommand

Specifies if the event’s arguments are passed to the command.

<ListBox x:Name="list">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="MouseDoubleClick" 
                               Command="{Binding EditCommand}" 
                               PassEventArgsToCommand="True"/>
    </dxmvvm:Interaction.Behaviors>
</ListBox>
EventArgsConverter

Allows you to maintain a clean MVVM pattern and convert the event’s arguments to an object suitable for the command.

<ListBox x:Name="list">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="MouseDoubleClick" 
                               Command="{Binding EditCommand}" 
                               EventArgsConverter="{dxmvvm:ItemsControlMouseEventArgsConverter}"/>
    </dxmvvm:Interaction.Behaviors>
</ListBox>

Pass a Parameter Back to the Bound Event

A parameter that a command processes may indicate to the source object how to implement a certain action. You can pass the parameter from the command to the bound event, so that the source object can determine how to proceed. Create a converter class that implements IEventArgsTwoWayConverter and assign the class object to the EventArgsConverter property.

The code sample below uses the back conversion to validate data:

<dxe:TextEdit EditValue="{Binding UserName}">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand Command="{Binding UserNameValidationCommand}"
                               EventArgsConverter="{local:ValidateEventArgsConverter}"
                               EventName="Validate"/>
    </dxmvvm:Interaction.Behaviors>
</dxe:TextEdit>
public class ValidationArgs {
    public string ErrorContent { get; private set; }
    public object Value { get; }

    public ValidationArgs(object value) => Value = value;
    public void SetError(bool isValid, string errorContent) => ErrorContent = isValid ? null : errorContent;
}

public class ValidateEventArgsConverter : EventArgsConverterBase<ValidationEventArgs> {
    protected override object Convert(object sender, ValidationEventArgs e) => new ValidationArgs(e.Value);
    protected override void ConvertBack(object sender, ValidationEventArgs e, object parameter) {
        var args = parameter as ValidationArgs;
        e.IsValid = args.ErrorContent == null;
        e.ErrorContent = args.ErrorContent;
    }
} 

Specify Modifier Keys as an Additional Condition of the Command Execution

Use the ModifierKeys property to specify keys a user should press to invoke the command.

In the code sample below, the bound EditCommand is invoked when a user clicks a ListBoxItem and presses the Ctrl and Alt keys.

<ListBox ItemsSource="{Binding Persons}">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="MouseLeftButtonUp" 
                               Command="{Binding EditCommand}" 
                               EventArgsConverter="{dxmvvm:ItemsControlMouseEventArgsConverter}" 
                               ModifierKeys="Ctrl+Alt"/>
    </dxmvvm:Interaction.Behaviors>
</ListBox>
Property Description
MarkRoutedEventsAsHandled Gets or sets whether the associated routed event is marked as handled when the command is executed. This is a dependency property.
DispatcherPriority Gets or sets the priority of the Dispatcher that invokes the associated command. This is a dependency property.
AllowChangingEventOwnerIsEnabled Gets or sets whether the event source object’s IsEnabled property value reflects the command’s CanExecute method value. This is a dependency property.
ProcessEventsFromDisabledEventOwner Gets or sets whether the EventToCommand processes events for the source object even if the source object is disabled. This is a dependency property.

The following code snippets (auto-collected from DevExpress Examples) contain references to the EventToCommand class.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

Inheritance

Show 13 items
Object
DispatcherObject
DependencyObject
Freezable
Animatable
DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase
DevExpress.Mvvm.UI.Interactivity.Behavior
DevExpress.Mvvm.UI.Interactivity.TriggerBase
DevExpress.Mvvm.UI.Interactivity.TriggerBase<DependencyObject>
EventTriggerBase<DependencyObject>
DevExpress.Mvvm.UI.Interactivity.EventTrigger
EventToCommandBase
EventToCommand
See Also