WPF:如何从ViewModel发出一个事件来查看代码中没有代码的代码? [英] WPF: how to signal an event from ViewModel to View without code in codebehind?

查看:214
本文介绍了WPF:如何从ViewModel发出一个事件来查看代码中没有代码的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很简单(我希望:))问题:



在MVVM中,View通常监听ViewModel属性的更改。不过,我有时会喜欢听事件,所以例如,当VM信号时,View可以启动动画或关闭窗口。



通过bool属性与NotifyPropertyChanged(只有当它从false更改为true时才启动动画)是可能的,但它感觉像一个黑客,我更喜欢暴露事件,因为它在语义上是正确的。



另外,我想在codebehind中没有代码,就像 viewModel.myEvent + = handler 这意味着我会手动取消注册事件,以便允许View为GC'd - WPF视图已经能够侦听属性弱,我会非常喜欢在View中以声明方式编程。



标准的强大事件订阅也不好,因为我需要为一个视图切换多个ViewModels(因为每次创建View太多的CPU时间)。



感谢您的想法(如果有标准解决方案,msdn的链接就足够了)!


<谢谢你指出你的问题。



使用混合行为和现有触发器和操作来满足Tomas的要求:



当底层Viewmodel指示时,如何在视图触发器上具有WPF EventTrigger应该?


I have quite simple (I hope :)) problem:

In MVVM, View usually listens on changes of ViewModel's properties. However, I would sometimes like to listen on event, so that, for example, View could start animation, or close window, when VM signals.

Doing it via bool property with NotifyPropertyChanged (and starting animation only when it changes from false to true) is possible, but it feels like a hack, I'd much prefer to expose event, as it is semantically correct.

Also, I'd like to do it without code in codebehind, as doing viewModel.myEvent += handler there would mean that I'd have manually unregister the event in order to allow View to be GC'd - WPF Views are already able to listen on properties 'weakly', and I'd much prefer to program only declaratively in View.

The standard strong event subscription is also bad, because I need to switch multiple ViewModels for one View (because creating View every time takes too much CPU time).

Thank you for ideas (if there is a standard solution, a link to msdn will suffice)!

解决方案

Thanks for pointing out your question.

Use blend behaviors and existing triggers and actions to fulfill Tomas's requirement:

How can I Have a WPF EventTrigger on a View trigger when the underlying Viewmodel dictates it should?

这篇关于WPF:如何从ViewModel发出一个事件来查看代码中没有代码的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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