C# 鼠标中键单击的XAML调用命令(System.Windows.Interactive)
C# 鼠标中键单击的XAML调用命令(System.Windows.Interactive),c#,wpf,xaml,C#,Wpf,Xaml,System.Windows.Interactivity允许在触发特定事件时调用命令,而无需编写代码。但是,当鼠标中键(滚轮)单击某个元素时,我找不到如何调用命令 <StackPanel> <i:Interaction.Triggers> <i:EventTrigger EventName="..."> <i:InvokeCommandAction Command="{Binding CloseComman
System.Windows.Interactivity
允许在触发特定事件时调用命令,而无需编写代码。但是,当鼠标中键(滚轮)单击某个元素时,我找不到如何调用命令
<StackPanel>
<i:Interaction.Triggers>
<i:EventTrigger EventName="...">
<i:InvokeCommandAction Command="{Binding CloseCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
...
</StackPanel>
...
您可以创建一个自定义的事件触发器
,为您处理此问题:
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>
内置的没有,因为没有为鼠标滚轮按钮引发特定事件。因为Ash指出单击滚轮按钮是一件事,它促使我研究如何工作。 你可以用鼠标夹
<StackPanel.InputBindings>
<MouseBinding Gesture="WheelClick" Command="{Binding WheelClickCommand}" />
</StackPanel.InputBindings>
正如Andy的回答所示,您可以使用鼠标绑定。但是,
WheelClick
手势表示滚动动作,请改用MiddleClick
<StackPanel>
<StackPanel.InputBindings>
<MouseBinding Gesture="MiddleClick" Command="{Binding CloseCommand}" />
</StackPanel.InputBindings>
...
</StackPanel>
...
大多数鼠标滚轮无法单击。因此,这不是一个被普通鼠标驱动程序识别为不同事件的事件。我可以给你一些东西,让你绑定鼠标滚轮滚动,或者你可以用谷歌搜索它。前一段时间我从网上抓到了一个实现。@Andy,中键点击是存在的,非常有用。好的。我自己从未见过实现。结果证明mousebinding可以与此配合使用。谢谢,这是一个优雅的解决方案!不幸的是,这不起作用,只有在滚动鼠标滚轮而不是单击鼠标滚轮时才会调用该命令。显然,还有一个手势MiddleClick
,似乎也能完成这项工作!上面的代码不仅仅是air代码——我试过的时候它还起作用。也许鼠标驱动程序或设置会有所不同。你可以在这里找到不同的鼠标操作:或者我在点击时旋转了一下。耸肩