如何使用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
发布评论