WPF MVVM:命令很容易。如何使用RoutedEvent连接View和ViewModel [英] WPF MVVM : Commands are easy. How to Connect View and ViewModel with RoutedEvent

查看:160
本文介绍了WPF MVVM:命令很容易。如何使用RoutedEvent连接View和ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个在资源字典中实现为DataTempate的视图。
我有一个相应的ViewModel。
绑定命令很容易。但是如果我的视图包含一个控件,如ListBox,如果我需要发布一个应用程序广泛的事件(使用Prism的事件Aggreagtor)基于列表上正在更改的项目。

Suppose I have a view implemented as a DataTempate inside a resource Dictionary. And I have a corresponding ViewModel. Binding Commands are easy. But what if my View contains a control such as a ListBox, and I need to Publish an application wide event (Using Prism's Event Aggreagtor) based on the Item being Changed on the List.

如果ListBox支持命令,我可以将其绑定到ViewModel中的命令并发布事件。但是Listbox不允许这样的选项。
如何桥接这个?

if ListBox supports a command I could just bind it to a command in the ViewModel and publish the event. But Listbox doesn't allow such an option. How do I bridge this?

编辑:
许多很棒的答案。

Many great answers.

查看此链接 http://blogs.microsoft.co.il/blogs/tomershamam/archive/2009/04/14/wpf-commands-everywhere.aspx

感谢

Ariel

推荐答案

问题并添加对您需要的特定命令的支持。例如,我修改ListView之前支持 ItemActivated 事件和相关命令。

One option is to extend the control in question and add support for the particular command you require. For example, I've modified ListView before to support the ItemActivated event and related command.

这篇关于WPF MVVM:命令很容易。如何使用RoutedEvent连接View和ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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