WPF MVVM doubts

Welcome StackOverflow users (or Stackoverflowers?):

I am involved in WPF programming. I read several articles / saw several screencasts and, based on the background of WEB dev, I released VS2010 and started making an example application that will help me learn the basics.

I also read something about MVVM and started using it. I installed my solution for using WPF 4.0, ActiveRecord 2.1 and SQLite, and everything went well. But I still have some doubts:

  • I created MainWindowViewModel and use the RelayCommand class from here to ... pass the command. Am I breaking any recommendations by having a MenuItem from MainWindow to bind its command to a property of this view model?

  • This action, with which I associate the MenuItem command, is about to instantiate a new ViewModel and a new view and show it. Again, is this normal in the context of MVVM?

  • My MainWindow will be a kind of dashboard, and I will have more than one model attached to this dashboard. Should I just transfer all of these models into one view model?

Something like that:

public class MainWindowViewModel { private ObservableCollection<Order> openOrders; private Address deliveryAddress; private Order newOrder; /* Wrappers for the OpenOrders Collection */ /* Wrappers for Delivery Address */ /* Wrappers for New Order */ /* Command Bindings */ } 

TIA!

+6
c # wpf mvvm
source share
3 answers

I created MainWindowViewModel and use the RelayCommand class here to ... pass the command. Am I breaking any recommendations by having a MenuItem from MainWindow to bind its command to a property of this view model?

No, you do not violate any recommendations. This is great for binding MenuItem to the MainWindowViewModel command (where else would you put this command anyway?)

This action, with which I associate the MenuItem command, will instantiate a new ViewModel and a new view and display it. Again, is this normal in the context of MVVM?

Of course, create a new ViewModel, of course. As for creating a new view, it depends on how you create it ... you, of course, should never specify the view explicitly from the ViewModel, because it will represent the dependence of the virtual machine on the view.

My MainWindow will be a kind of dashboard, and I will have more than one model attached to this dashboard. Should I just transfer all of these models into one view model?

It depends on what you mean by "wrap" ... Your MainWindowViewModel can display other ViewModels through properties, and VM abstracts will be displayed in different parts of the view. If this is what you mean, yes, you should wrap them.

+6
source share

Addendum to Thomas's answer:

I would create different user controls for each part of the control panel and assign a viewModel for each user control.

+3
source share

I created MainWindowViewModel and use the RelayCommand class here to ... pass the command. Am I breaking any recommendations by having a MenuItem from MainWindow to bind its command to a property of this view model?

No, exactly where you add commands.

This action, with which I associate the MenuItem command, will instantiate a new ViewModel and a new view and display it. Again, is this normal in the context of MVVM?

No need to know how to instantiate a new view; what a viewing task. The specifics of how to do this depends on how you display this new view - it can be as simple as having a ContentPresenter in the view associated with a property in the view model, so when you set the property (and raise the PropertyChanged) ContentPresenter displays a new object with the corresponding DataTemplate.

Everything becomes a little strange if “create a new view” means “open a new window”. There is no particularly elegant way to do this, especially if you want the new window to be a modal dialog. One way is to add an event handler to the view code that listens for PropertyChanged in the view model; when the property of the subview model is set, the code in the view creates and displays a new window.

My MainWindow will be a kind of dashboard, and I will have more than one model attached to this dashboard. Should I just transfer all of these models into one view model?

Of course. This is a very common picture. It is not at all uncommon, for example, to set the visible property of a collection and associate with it some ItemControl; The view will automatically create views for each view model that you have embedded in this collection. Again, the specific implementation really depends on your application.

+2
source share

All Articles