Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events rather than Commands in MVVM?

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?

like image 290
Hasan Fahim Avatar asked May 26 '11 11:05

Hasan Fahim


1 Answers

In general, if your code pertains to UI logic, keep it in the view's XAML or code-behind. The view model is only responsible for bridging and binding data between the view and the model.

An example can be found in one of my questions, How do I make a WPF window movable by dragging the extended window frame? One of the events I use is SourceInitialized, in which I access the Window's window handle to perform some Windows API magic. But all this relates to the window, and has nothing to do with application logic beyond the window, so I restrict it all to the window's code-behind file, leaving the view model completely ignorant of it.

like image 69
BoltClock Avatar answered Oct 03 '22 21:10

BoltClock