Events rather than Commands in MVVM?

2019-06-19 00:10发布

It is often specified in the various tutorials of MVVM, that the goal of MVVM is not to eliminate the code-behind and that some event-handling may still be necessary in the code-behind.

What are the scenarios in which you need to write events in code-behind rather than using commands in viewmodel?

8条回答
Bombasti
2楼-- · 2019-06-19 00:53

I didn't find a nice way to handle binding the selection of multiple items in listboxes and had to do it in code behind. But it's not "un-clean"

查看更多
三岁会撩人
3楼-- · 2019-06-19 00:57

I would say any “logic” that would needs to be different if you were porting to another desktop UI (e.g. the mac) should be in the code behind. (E.g. other platform with about the same logical UI needs)

So hooking all the events to detach when the use is “hovering” over an input field should be in the code behind, but deciding what to show in the “status line” when a user does hover should be in the view model.

查看更多
登录 后发表回答