简体   繁体   中英

WPF - Dialogs using MVVM

When using custom dialog windows in a MVVM application, do you think it´s Ok to use the code behind to handle properties, events etc.? Or should I always have a ViewModel binded to every dialog view? What´s your thought?

The reasons for having a ViewModel on a dialog are pretty much the same as those for having them on any other kind of window as far as I'm concerned, so I'd do the same thing (whatever that is!). Personally, I'd have the ViewModel and as little as possible in the code behind.

By which criterion should we decide whether code-behind is OK or not?

What if the custom dialog is a complex wizard? What if the dialog is an entire mini-application in its own right? In those cases I think we can argue that MVVM is worthwhile, and code-behind really isn't very attractive.

If we accept that, then where do we draw the line? I think we shouldn't, because a dialog which is simple today may become complex in the future . It would be better to stay consistent.

In other words: MVVM all the way

See also this related question: MVVM and commands that show more GUI

A dialog is just another view. The primary purpose of the ViewModel is to encapsulate the behavior of the view. If that said view has behavior, than a ViewModel is warranted, in my opinion. If it is a static UI, or simply bound directly to a model item without any coded behavior, than no, a ViewModel is not necessary.

Why not to use a ViewModel for a dialog?

The ViewModel sample application of the WPF Application Framework (WAF) shows how to use WPF dialogs together with ViewModels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM