EventToCommand Class
Executes a command in response to a raised event.
Namespace: DevExpress.WinUI.Core
Assembly: DevExpress.WinUI.Core.v22.1.dll
NuGet Package: DevExpress.WinUI
Declaration
Remarks
Specify an Event Owner
An event owner is an object that raises events. The default event owner object is a control associated with the EventToCommand behavior.
<UserControl ...
xmlns:dx="using:DevExpress.WinUI.Core">
<ListBox>
<dx:Interaction.Behaviors>
<dx:EventToCommand EventName="DoubleTapped" Command="{x:Bind ShowDetailCommand}"/>
</dx:Interaction.Behaviors>
</ListBox>
</UserControl>
You can use the EventOwner property to specify a different event owner object for the EventToCommand.
<UserControl ...
xmlns:dx="using:DevExpress.WinUI.Core">
<dx:Interaction.Behaviors>
<dx:EventToCommand EventOwner="{x:Bind list}" EventName="DoubleTapped" Command="{x:Bind ShowDetailCommand}"/>
</dx:Interaction.Behaviors>
<!-- ... -->
<ListBox x:Name="list" ... />
</UserControl>
Specify an Event
Use the EventName property to specify an event for the EventToCommand behavior:
<UserControl ...
xmlns:dx="using:DevExpress.WinUI.Core">
<dx:Interaction.Behaviors>
<dx:EventToCommand EventName="Loaded" Command="{x:Bind LoadedCommand}" />
</dx:Interaction.Behaviors>
<!-- ... -->
</UserControl>
The default EventName property value is Loaded
.
Pass a Parameter to the Bound Command
Use the CommandParameter property to pass a parameter to a command:
<ListBox x:Name="list">
<dx:Interaction.Behaviors>
<dx:EventToCommand EventName="DoubleTapped" Command="{x:Bind ShowDetailCommand}"
CommandParameter="{x:Bind list.SelectedItem, Mode=OneWay}"/>
</dx: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" ...> <dx:Interaction.Behaviors> <dx:EventToCommand EventName="DoubleTapped" Command="{x:Bind ShowDetailCommand}" PassEventArgsToCommand="True"/> </dx: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 ...> <dx:Interaction.Behaviors> <dx:EventToCommand EventName="DoubleTapped" Command="{x:Bind EditCommand}"> <dx:EventToCommand.EventArgsConverter> <local:CustomEventArgsConverter/> </dx:EventToCommand.EventArgsConverter> </dx:EventToCommand> </dx:Interaction.Behaviors> </ListBox>
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 ShowDetailCommand is invoked when a user presses Alt and clicks or taps a ListBoxItem.
<ListBox ...>
<dx:Interaction.Behaviors>
<dx:EventToCommand EventName="Tapped" Command="{x:Bind ShowDetailCommand}" ModifierKeys="Alt">
<dx:EventToCommand.EventArgsConverter>
<local:ListBoxPointerEventArgsConverter/>
</dx:EventToCommand.EventArgsConverter>
</dx:EventToCommand>
</dx:Interaction.Behaviors>
</ListBox>
Related API
Property | Description |
---|---|
AllowChangeEventOwnerIsEnabled | Gets or sets whether the event owner object’s IsEnabled property value reflects the command’s CanExecute method value. This is a dependency property. |
DispatcherPriority | Gets or sets the priority of the DispatcherQueue that invokes the associated command. The default value is null which means that the DispatcherQueue is not used to invoke the associated command. This is a dependency property. |
MarkRoutedEventsAsHandled | Gets or sets whether the associated routed event is marked as handled when the command is executed. This is a dependency property. |
PassEventArgsToCommand | Gets or sets whether the event arguments are passed to the command. This is a dependency property. |
ProcessEventsFromDisabledEventOwner | Gets or sets whether the EventToCommand processes events for event owner objects even if they are disabled. This is a dependency property. |