Im just getting started on this whole WPF thing, and need to understand some basic elements. I gotten so far that i undrstand the Model, the View and the ViewModel in the MVVM pattern.
What i dont get is where/how to go about chaning my UI based on this.
Lets say i have an application where i need to login first. my login is a view and a viewmodel+model i have. No problem in showing this one, because its the “startup” view i simply add it in my window’s constructor (right or wrong?). When the user has successfully logged in, i want to change the view and the viewmodel to eg. a dashboard view+viewmodel. Should i do all this manually, just removing the usercontrol that was the login, then adding the other usercontrol that is the dashboard? and then change the datacontext? How is this normally accomplished?
My concern is that im introducing a lot of code simply to switch view+viewmodel, and this bothers me a bit. What when i go from dashboard to customerlisting, then to projectlisting etc. do I write this logic in my eventhandler /command for the button pressed?
Your login window would have its own view model and your dashboard would have its own view model. You don’t switch view models manually.
So the user enters his/her username and password and hits enter, the login view model gets the data via binding in the view then the model validates the credentials, if they’re correct then the dashboard appears which is bound to its view model.
You have a login view and view model + dashboard view and view model + other view and view models for other controls.
— Update:
Let’s say you have the following:
– Window
– Login control
– Dashboard control
All have their own views (V) and view models (VM)
The Window VM has two visibility properties, the Login and the Dashboard visibility properties in the Window V bind to them.
When the app starts the Login control is visible, when the login is successful, the model tells the Window VM to set the Login visibility property to collapsed and the Dashboard visibility property to visible, which then the Window VM tells the Window V to hide the Login control and show the Dashboard control.
“tells” means raise event, send a message, call a method – depends on how you implement MVVM.