wpf-devexpress-dot-mvvm-dot-ui-7ed4d8fb.md
Executes a command in response to a raised event.
Namespace : DevExpress.Mvvm.UI
Assembly : DevExpress.Xpf.Core.v25.2.dll
NuGet Package : DevExpress.Wpf.Core
public class EventToCommand :
EventToCommandBase
Public Class EventToCommand
Inherits EventToCommandBase
Review the following example for information about the EventToCommand :
View Example: How to: Use EventToCommand
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 :
SourceObjectAllows you to bind the behavior’s source object to a control.SourceNameSpecifies 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>
You can use one of the following properties to specify an event:
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>
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>
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:
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>
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>
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;
}
}
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.
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="DragAppointmentOver"
Command="{Binding OnDragAppointmentOverCommand}"
reporting-wpf-mvvm-how-to-validate-editing-fields/CS/MainWindow.xaml#L16
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand Event="{x:Static dxp:DocumentPreviewControl.ShowingEditingFieldEditorEvent}"
PassEventArgsToCommand="True"
reporting-wpf-drill-through/CS/Viewer/MainWindow.xaml#L36
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="DocumentPreviewMouseClick" PassEventArgsToCommand="True" Command="{Binding OnPreviewMouseClickCommand}" />
<dxmvvm:EventToCommand EventName="DocumentPreviewMouseMove" PassEventArgsToCommand="True" Command="{Binding OnPreviewMouseMoveCommand}" />
wpf-diagram-mdi/CS/MainWindow.xaml#L40
<local:DiagramService />
<dxmvvm:EventToCommand Command="{Binding OnLoadedCommand}" EventName="Loaded" />
</dxmvvm:Interaction.Behaviors>
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand Command="{Binding AppointmentsUpdatedCommand}" EventName="AppointmentsUpdated" />
</dxmvvm:Interaction.Behaviors>
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