EventSetter Command Purpose

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

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

Try the Marlon Grech attached behavior commands , as mentioned in this previous question.

Alternatively, as a simpler but less flexible solution, execute the implementation Handlerin code to raise the command directly, for example:

<!-- 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);
}
+2
source

Source: https://habr.com/ru/post/1769346/


All Articles