开发者

How to change View + viewmodel based on command

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 si开发者_如何转开发mply 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?


  • the view is the UI (could be a window, page, usercontrol)
  • the view model is the back-end to that view

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.


Create MainPageView with containing a user control and a Frame. Set MainPageView as your root visual. You can then bind Frame Source to a property in the MainPageViewModel. Then navigating between login or dashboard is a matter of sending a message to the MainPageViewModel and setting the property that the frame source is bound to.


If you want your view to be in another window, then you can simply create your view/viewmodel in each window (separate XAML) and switch between them by creating the new window, making it visible, and then exiting the previous.

However, if you want to switch a view within the same window, then I recommend you do so using a custom ContentPresenter. I have asked a similar question on this, which you can find here: Resource management when changing window content.

If you simply toggle visibility, you will be holding onto the loaded resources for all views. Using a custom ContentPresenter, you can swap out the current content, and the old content will be picked up by the garbage collector. A command would be sent and picked up by the view model, which will change the current view field (this is a field in the view model). The ContentPresenter will have a binding to the view model's field, automatically switching its content when the View Model receives the command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜