Assigning a command to the EventSetter

7.5k Views Asked by At

How to assign a command to the Handler in EventSetter, I want to to write this:

<Style x:Key="ItemStyle" TargetType="{x:Type ListBoxItem}">
            <EventSetter Event="PreviewMouseDoubleClick" Handler="{Binding MyDoubleClickCommand}"/>            
1

There are 1 best solutions below

0
On

Try Marlon's Grech's attached commands behaviours, as mentioned in this previous question.

Alternatively, as a simpler but less flexible solution, provide a Handler implementation in the code-behind to raise the command directly, like this:

<!-- In the XAML -->
<EventSetter Event="PreviewMouseDoubleClick" Handler="MyPreviewDoubleClickHandler"/>

// In the code-behind
private void MyPreviewDoubleClickHandler(object sender, RoutedEventArgs args) {
    object my_param = ...;
    MyCommand.Execute(my_param, this);
}