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

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

问题描述

在使用MVVM的WPF应用程序中,我有一个带listview项的usercontrol.在运行时,它将使用数据绑定将对象集合填充到列表视图中.

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:

WPF应用程序框架(WAF)- http://waf.codeplex.com

WPF Application Framework (WAF) - http://waf.codeplex.com

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

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