双击 ListView 时执行命令。 (WPF-MVVM)

发布于 2024-11-28 19:38:45 字数 618 浏览 0 评论 0原文

我在将命令 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-12-05 19:38:45

ListView 的默认 Click 事件将事件标记为已处理。尝试使用 PreviewLeftClickPreviewLeftDoubleClick 代替

编辑

,因为 MouseBindings 不包含 PreviewLeftClickPreviewLeftDoubleClick >,尝试使用发现的AttachedCommandBehavior代码此处,允许您附加命令 几乎任何 Event

例如,

<ListView local:CommandBehavior.Event="PreviewMouseDown" 
          local:CommandBehavior.Command="{Binding OpenSOACommand}" />

The default Click event for the ListView is marking the event as handled. Try using PreviewLeftClick and PreviewLeftDoubleClick instead

EDIT

Since MouseBindings does not contain a PreviewLeftClick or PreviewLeftDoubleClick, try using the AttachedCommandBehavior code found here which allows you to attach a Command to just about any Event

For example,

<ListView local:CommandBehavior.Event="PreviewMouseDown" 
          local:CommandBehavior.Command="{Binding OpenSOACommand}" />
┼── 2024-12-05 19:38:45

这是因为 ListView 的 ListViewItems 会吞噬您的 LeftClick 事件并将它们转换为漂亮的 SelectionChanged 事件。由于 ListViewItems 不会响应 MiddleClick,因此这将按预期工作。

您可能希望通过处理该事件的匹配预览等效项来获得此点击的“前面”。

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <EventSetter Event="MouseDoubleClick" Handler="OnItemDoubleClick"/>
    </Style>
</ListView.ItemContainerStyle>

并在处理程序中调用命令:

private void OnItemDoubleClick(object sender, MouseButtonEventArgs e)
{
     OpenSOACommand.Execute(null, this);
}

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.

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <EventSetter Event="MouseDoubleClick" Handler="OnItemDoubleClick"/>
    </Style>
</ListView.ItemContainerStyle>

And invoke the command in the handler:

private void OnItemDoubleClick(object sender, MouseButtonEventArgs e)
{
     OpenSOACommand.Execute(null, this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文