Passing event args and sender to the RelayCommand

前端 未结 3 1555
天命终不由人
天命终不由人 2020-12-18 22:39

How do you get event sender when using RelayCommand?

3条回答
  •  离开以前
    2020-12-18 23:43

    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:

    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.

提交回复
热议问题