Hello fellow StackOverflow users (or Stackoverflowers?):
I’m learning-by-coding WPF. I read several articles/saw several screencasts, and coming from a WEB dev background, I fired up VS2010 and started doing a sample application that would help me learn the basics.
I read some about MVVM too, and started using it. I set up my solution to use WPF 4.0, ActiveRecord 2.1 and SQLite, and everything went kind well. But I still have some doubts:
-
I created a MainWindowViewModel, and am using the RelayCommand class from here to… relay the command. Am I breaking any guidelines by having a MenuItem from the MainWindow to have its command bound to a property of this viewmodel?
-
This action I’m binding the MenuItem command to is going to instantiate a new ViewModel and a new View, and show it. Again, is that ok in the MVVM context?
-
My MainWindow will be a kind of “dashboard”, and I will have more than one model attached to this dashboard. Should I just wrap all those models in a single view model?
Something like this:
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!
No, you’re not breaking any guideline. It’s perfectly appropriate to bind the MenuItem to a command of the MainWindowViewModel (where else would you put this command anyway ?)
It’s perfectly fine to create a new ViewModel, of course. As for creating a new view, it depends on how you create it… you should of course never instantiate a view explicitly from the ViewModel, because it would introduce a dependency of the VM to the view.
It depends on what you mean by “wrap”… Your
MainWindowViewModelcould expose other ViewModels through properties, and theses VMs would be displayed in different parts of the view. If that’s what you mean, yes, you should wrap them.