将命令分配给EventSetter

时间:2010-10-13 13:43:53

标签: .net binding eventsetter

如何在EventSetter中为Handler分配命令,我想写这个:

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

1 个答案:

答案 0 :(得分:3)

尝试Marlon的Grech attached commands behaviours,如previous question.

中所述

或者,作为一种更简单但不太灵活的解决方案,在代码隐藏中提供Handler实现以直接引发命令,如下所示:

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