使用 MVVM 从 WPF ListView 项目触发双击事件 [英] Firing a double click event from a WPF ListView item using MVVM

查看:43
本文介绍了使用 MVVM 从 WPF ListView 项目触发双击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 MVVM 的 WPF 应用程序中,我有一个带有列表视图项的用户控件.在运行时,它将使用数据绑定用一组对象填充列表视图.

In a WPF application using MVVM, I have a usercontrol with a listview item. In run time, it will use databinding to fill the listview with a collection of objects.

将双击事件附加到列表视图中的项目的正确方法是什么,以便在双击列表视图中的项目时,会触发视图模型中的相应事件并引用单击的项目?

What is the correct way to attach a double click event to the items in the listview so that when an item in the list view is doubleclicked, A corresponding event in the view model is fired and has a reference to the item clicked?

如何以干净的 MVVM 方式完成,即视图中没有代码?

How can it be done in a clean MVVM way i.e. no code behind in the View?

推荐答案

拜托,代码隐藏并不是一件坏事.不幸的是,WPF 社区中有很多人都弄错了.

Please, code behind is not a bad thing at all. Unfortunately, quite a lot people in the WPF community got this wrong.

MVVM 不是消除背后代码的模式.就是将视图部分(外观、动画等)与逻辑部分(工作流)分离.此外,您可以对逻辑部分进行单元测试.

MVVM is not a pattern to eliminate the code behind. It is to separate the view part (appearance, animations, etc.) from the logic part (workflow). Furthermore, you are able to unit test the logic part.

我知道足够多的场景,您必须在背后编写代码,因为数据绑定并不是解决所有问题的方法.在您的场景中,我将在代码隐藏文件中处理 DoubleClick 事件并将此调用委托给 ViewModel.

I know enough scenarios where you have to write code behind because data binding is not a solution to everything. In your scenario I would handle the DoubleClick event in the code behind file and delegate this call to the ViewModel.

可以在此处找到使用隐藏代码并仍然实现 MVVM 分离的示例应用程序:

Sample applications that use code behind and still fulfill the MVVM separation can be found here:

Win 应用程序框架 (WAF) - https://github.com/jbe2277/waf

Win Application Framework (WAF) - https://github.com/jbe2277/waf

这篇关于使用 MVVM 从 WPF ListView 项目触发双击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆