用于鼠标中键的XAML调用命令(System.Windows.Interactivity)

时间:2018-04-06 13:02:48

标签: c# wpf xaml

System.Windows.Interactivity允许在触发特定事件时调用命令,而无需编写代码。但是,当单击元素上的鼠标中键(滚轮)时,我无法找到如何调用命令。

<StackPanel>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="...">
            <i:InvokeCommandAction Command="{Binding CloseCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    ...
</StackPanel>

3 个答案:

答案 0 :(得分:1)

因为Ash指出单击滚轮按钮是一件事,它促使我调查它是如何工作的。 你可以使用鼠标绑定。

<StackPanel.InputBindings>
    <MouseBinding Gesture="WheelClick" Command="{Binding WheelClickCommand}" />
</StackPanel.InputBindings>

答案 1 :(得分:0)

您可以创建一个自定义EventTrigger来处理此问题:

public class MouseWheelButtonEventTrigger : System.Windows.Interactivity.EventTrigger
{
    public MouseWheelButtonEventTrigger()
    {
        EventName = "MouseDown";
    }

    protected override void OnEvent(EventArgs eventArgs)
    {
        MouseButtonEventArgs mbea = eventArgs as MouseButtonEventArgs;
        if (mbea != null && mbea.ChangedButton == MouseButton.Middle)
            base.OnEvent(eventArgs);
    }
}

样本用法:

<StackPanel Background="Yellow" Width="100" Height="100">
    <i:Interaction.Triggers>
        <local:MouseWheelButtonEventTrigger>
            <i:InvokeCommandAction Command="{Binding CloseCommand}" />
        </local:MouseWheelButtonEventTrigger>
    </i:Interaction.Triggers>
</StackPanel>

内置的没有,因为没有为鼠标滚轮按钮引发特定事件。

答案 2 :(得分:0)

正如Andy的回答所示,你可以使用鼠标绑定。但是,WheelClick手势表示滚动操作,请改用MiddleClick

<StackPanel>
    <StackPanel.InputBindings>
        <MouseBinding Gesture="MiddleClick" Command="{Binding CloseCommand}" />
    </StackPanel.InputBindings>
    ...
</StackPanel>