Events rather than Commands in MVVM?
Asked Answered
P

8

6

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?

Petrillo answered 26/5, 2011 at 11:31 Comment(0)
S
7

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.

Syntax answered 26/5, 2011 at 11:34 Comment(1)
Notice how my question (and my own answer to it) say absolutely nothing about MVVM; besides the fact that WPF and MVVM aren't exactly synonymous, it also says much about the scope of the code found in them.Syntax
I
5

In my opinion, in MVVM, when events related to UI, something like animation , you can write in the code-behind file.

All the business logic should be in view model.

Itchy answered 26/5, 2011 at 11:33 Comment(0)
S
5

In my experience, using 3rd party controls that do not support MVVM binding will lead to writing code in code behind file. This happened even for simple usages such as selecting current item, reading currently selected item, etc. which should be fairly simple to implement in the control but has not.

A sample of this is SelectedItem property of Silverlight TreeView control, which instead of being DependencyProperty (being bindable) is a regular property so you can not bind to.

Also as @BoltClock mentioned, sometimes it seems logical to put some code in code behind which are really related to what the view does and has nothing to do with the logic "behind" the view. It is best to put these kinds of logic in the code-behind.

Swellhead answered 26/5, 2011 at 11:43 Comment(0)
H
4

You are always going to run into purists that say that there should never be ANY code in the code-behind file. I'm usually a purist, but not in this case.

If you have logic that is very specific to the view, it should go in the code-behind file. When I write complicated views that need to make large changes to the structure of the visual tree based on properties or changes in the view model, I put this code in the view. The view model should not know anything about the view, so it can't (and shouldn't) control these changes directly. Sometimes these changes can be implemented with Triggers in a Style or DataTemplate, but sometimes good old fashioned code is the best way.

Hoseahoseia answered 26/5, 2011 at 11:50 Comment(0)
C
3

I think that there is the possibility of Custom user controls using code behind.

Let's say you create a Closable Tab Item class that inherits TabItem. You might handle that closing action using events and tie it to a DP.

This is of course generic and could be used multiple times.

So I guess code behind is acceptable when the event has to do with the UI and not the dataModel or other activity.

Circinus answered 26/5, 2011 at 11:36 Comment(0)
A
3

One scenario I have come across is with 3'rd party controls. Some 3'rd party controls like telerik grid internally use there own custom data types to represent the grid rows etc and you need to handle the various grid events to convert those UI specific types to your custion types and then pass them to VM.

Amalle answered 26/5, 2011 at 11:44 Comment(0)
P
2

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.

Prather answered 26/5, 2011 at 11:48 Comment(3)
How is this relevant when you are targeting the .NET framework? If the framework is supported in mac, linux, etc, then the WPF calls into will also be supported. Unless there are specific features that are only partially supported (haven't followed mono, so I wouldn't know).Hoseahoseia
@Josh, it helps you think about what is part of the app UI logic and what is just "look and feel".Prather
And you are saying that the "look and feel" belongs in the UI and the UI logic could go in the VM? Makes sense to me.Hoseahoseia
A
0

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"

Andreasandree answered 29/5, 2011 at 7:4 Comment(2)
you can implement selection of multiple listbox items without code-behind. It is clean and works great. You can refer to the following link for the solution grokys.blogspot.com/2010/07/…Petrillo
@Hasan, thank you! just what I needed. Had trouble getting two-way binding of multi-selects to work, I think this will be much better!Andreasandree

© 2022 - 2024 — McMap. All rights reserved.