传递事件参数和发送者的RelayCommand [英] Passing event args and sender to the RelayCommand

查看:261
本文介绍了传递事件参数和发送者的RelayCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用RelayCommand当你发送的事件?

How do you get event sender when using RelayCommand?

推荐答案

这是其中的一个痛苦的,任何─%¤#的答案,我并没有真正回答你的问题,而是你说教什么,你应该做不同。所以,我们对此深感抱歉。这里有云:

This is one of those pain-in-the-%¤# answers where I don't actually answer your question, but instead lecture you about what you should be doing differently. So, sorry about that. Here goes:

如果你发现自己在你需要得到在您的视图模型发送对象的位置,那么你或许应该做不同的事情。通过引用,说,一个按钮或在您的视图模型的列表框,你所做的视图模型了解用户界面的概念,它不应该知道的事情。我的建议是不是挂钩到在code-隐藏页面的情况下,找出你需要知道发送者或什么,而不是,然后以编程方式执行视图模型命令。这也打破了MVVM规则,但恕我直言,这是一个非常细微的黑客比引用发件人在你的视图模型。

If you find yourself in a position where you need to get at the sender object in your viewmodel, then you should probably do something different. By referencing, say, a Button or a ListBox in your viewmodel you have made that viewmodel aware of UI concepts that it should not know anything about. My suggestion is to instead hook up to the event in the code-behind page, find out what you need to know about the sender or what-not, and then execute the viewmodel command programmatically. That is also breaking the MVVM "rules", but IMHO it is a much slighter hack than referencing the sender in your viewmodel.

这篇关于传递事件参数和发送者的RelayCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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