双击 ListView 时执行命令。 (WPF-MVVM)
我在将命令 (ICommand) 绑定到 ListView 的 MouseBinding 时遇到一些困难。 我使用这段 XAML 代码来测试不同的鼠标手势:
<ListView.InputBindings>
<MouseBinding Command="{Binding OpenSOACommand}" Gesture="LeftClick" />
<MouseBinding Command="{Binding OpenSOACommand}" Gesture="MiddleClick" />
<MouseBinding Command="{Binding OpenSOACommand}" Gesture="LeftDoubleClick" />
</ListView.InputBindings>
LeftClick 和 LeftDoubleClick 手势未触发,但 MiddleClick 鼠标绑定工作正常(我也一次测试了一个鼠标绑定...)。
LeftDoubleClick 和 MiddleClick 手势的处理方式有区别吗?如果有,我如何将 ICommand 绑定到 LeftDoubleClick 手势?
谢谢!
I am having some difficulties binding a command (ICommand) to the MouseBinding of a ListView.
I used this piece of XAML code to test the different mouse gestures:
<ListView.InputBindings>
<MouseBinding Command="{Binding OpenSOACommand}" Gesture="LeftClick" />
<MouseBinding Command="{Binding OpenSOACommand}" Gesture="MiddleClick" />
<MouseBinding Command="{Binding OpenSOACommand}" Gesture="LeftDoubleClick" />
</ListView.InputBindings>
The LeftClick and LeftDoubleClick gestures aren't triggered, yet the MiddleClick mouse binding works perfect (I have tested the mouse bindings one at a time as well...).
Is there a difference in the way the LeftDoubleClick and MiddleClick Gesture is handled? And if there is, how can I bind my ICommand to the LeftDoubleClick gesture?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ListView 的默认
Click
事件将事件标记为已处理。尝试使用PreviewLeftClick
和PreviewLeftDoubleClick
代替编辑
,因为
MouseBindings
不包含PreviewLeftClick
或PreviewLeftDoubleClick
>,尝试使用发现的AttachedCommandBehavior
代码此处,允许您附加命令
几乎任何Event
例如,
The default
Click
event for the ListView is marking the event as handled. Try usingPreviewLeftClick
andPreviewLeftDoubleClick
insteadEDIT
Since
MouseBindings
does not contain aPreviewLeftClick
orPreviewLeftDoubleClick
, try using theAttachedCommandBehavior
code found here which allows you to attach aCommand
to just about anyEvent
For example,
这是因为 ListView 的 ListViewItems 会吞噬您的 LeftClick 事件并将它们转换为漂亮的 SelectionChanged 事件。由于 ListViewItems 不会响应 MiddleClick,因此这将按预期工作。
您可能希望通过处理该事件的匹配预览等效项来获得此点击的“前面”。
并在处理程序中调用命令:
This is because your ListViewItems of your ListView will swallow your LeftClick events and convert them into nice SelectionChanged events. Since the ListViewItems will not respond to MiddleClick, this will work as expected.
You might want to get 'in front' of this click by handling the matching Preview equivalent of the event.
And invoke the command in the handler: