将 ViewModel 命令绑定到 WPF DataGrid

发布于 2025-01-01 07:27:00 字数 99 浏览 3 评论 0原文

是否有一种友好且快速的方法将命令绑定到 WPF DataGrid 双击事件?

我使用过搜索框,但我找到的所有结果都有很多行代码,而且不太实用。

Is there a friendly and fast way of bind a command to a WPF DataGrid double click event?

I've used the search box, but all the results I've found had a lot of lines of code and weren't that practical to use.

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

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

发布评论

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

评论(2

帝王念 2025-01-08 07:27:00

这是实现模型-视图-视图模型 (MVVM) 的普遍愿望,但开箱即用并不容易。我还没有使用过它,但据我所知,使用 MVVM Light Toolkit 可以让你做这样的事情。

或者,如果您不希望这样,还有其他方案: WPF:如何使用 MVVM 将命令绑定到 ListBoxItem?

但是,一般来说,没有快速简便的方法来执行此操作。

This is a common desire for implementing Model-View-ViewModel (MVVM) in general, and not easily available out of the box. I haven't used it, but from what I've seen, using the MVVM Light Toolkit will allow you to do things like this.

Or, if you don't want that, there are other schemes as well: WPF: How to bind a command to the ListBoxItem using MVVM?

But, generally speaking, there's no quick and easy ways to do this.

爱要勇敢去追 2025-01-08 07:27:00

您不能“绑定”事件,事件属于代码隐藏。有一些解决方法,您可以创建一些自定义行为等,但恕我直言,如果您跳过这些障碍,那么您没有正确执行 MVVM,您正在尝试跨越事件驱动和 MVVM 之间的界限。

作为一个简单的解决方法,您可以简单地添加一个事件处理程序来调用视图模型上的方法。

You can't "Bind" events, events belong in the code-behind. There are some work-arounds, you could create some custom behaviors and such, but IMHO if you're jumping through those hoops, you're not doing MVVM properly, you're trying to straddle the fence between event driven and MVVM.

As a simple work around, you can simply add an event handler that calls a method on your view model.

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