如何在 WPF Datagrid 上拥有 MouseUp 和 MouseDoubleClick 事件

发布于 2024-10-03 04:25:34 字数 310 浏览 2 评论 0原文

问题就在这里。我在 WPF 项目上使用 MVVM 并使用 MVVM light。我在用户控件中有一个网格,它返回搜索结果。我希望用户能够单击网格并使该行可供父视图使用(从菜单中)并拥有它,以便用户可以双击该行并在新的“窗口”中打开。就我个人而言,我让这些项目正常工作,但是我无法让两者都工作。我试图将 1 个命令绑定到 MouseUp,另一个命令绑定到 MouseDoubleClick,但 MouseDoubleClick 事件永远不会被触发。如何在 MVVM 设置中使用 mouseUp 和 MouseDoubleClick 事件?或任何其他建议,以便能够从数据网格中选择一行以供菜单项使用并能够双击。

Here is the problem. I am using MVVM on a WPF project and using MVVM light. I have a grid in a user control that returns results from a search. I want the users to be able to click on the grid and have the row available for the parent view to use (from a menu) and have it so users can double click on the row and open in a new "window". Individually I have these items work properly, however I can not get both to work. I am trying to tie 1 command to the MouseUp and another to MouseDoubleClick but the MouseDoubleClick event never gets fired. How can I be able to use the mouseUp and MouseDoubleClick events in a MVVM setup? or any other suggestions to be able to select a row from the datagrid to be available to menu items and to be able to doubleclick on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

帅哥哥的热头脑 2024-10-10 04:25:34

使用 MVVMLight eventto 命令将为您提供选择更改和鼠标双击事件。

Using MVVMLight the eventtocommand will get you selectionchanged and mousedoubleclicked events.

高跟鞋的旋律 2024-10-10 04:25:34

我认为您将必须手动检测双击。

dblClickTimeOut = null;
row.onmouseup = function() {
    if( dblClickTimeOut == null)
        dblClickTimeOut = setTimeout("dblClickTimeOut = null; selectRow('"+this.id+"');",200);
    else {
        // double-click stuff
    }
}
selectRow = function() {
    // single-click stuff
}

I think you're going to have to manually detect a double-click.

dblClickTimeOut = null;
row.onmouseup = function() {
    if( dblClickTimeOut == null)
        dblClickTimeOut = setTimeout("dblClickTimeOut = null; selectRow('"+this.id+"');",200);
    else {
        // double-click stuff
    }
}
selectRow = function() {
    // single-click stuff
}
〆凄凉。 2024-10-10 04:25:34

对于那些喜欢短途方式的人(也对 mvvm 友好!),
在您的 xaml 类中定义交互性:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

在您的网格中:

<Grid>
  <Grid.InputBindings>
     <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ElementName=xyz, Path=DataContext.MouseDoubleClick}"/>
  </Grid.InputBindings>
  <i:EventTrigger EventName="MouseUp">
     <cmd:EventToCommand Command="{Binding ElementName=xyz, Path=DataContext.MouseUpEvent}"/>
  </i:EventTrigger>
</Grid>

希望有所帮助。

For those who love short ways (also mvvm friendly!),
define the interactivity in your xaml-class:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

And in your Grid:

<Grid>
  <Grid.InputBindings>
     <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ElementName=xyz, Path=DataContext.MouseDoubleClick}"/>
  </Grid.InputBindings>
  <i:EventTrigger EventName="MouseUp">
     <cmd:EventToCommand Command="{Binding ElementName=xyz, Path=DataContext.MouseUpEvent}"/>
  </i:EventTrigger>
</Grid>

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文