Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF MVVM navigate views

I have a WPF application with multiple views. I want to switch from view 1 to view 2 and from there I can switch to multiple views. So I want a button on view 1 that loads view2 in the same window.

I tried those things, but can't get it to work.

  • How to navigate through windows with MVVM Light for WPF?
  • https://galasoft.ch/posts/2011/01/navigation-in-a-wp7-application-with-mvvm-light

From the first link, the problem is that I don't understand the ViewModelLocator code. They call the CreateMain(); function but where is this defined, and how can I switch to another view from inside a view.

like image 413
user2499088 Avatar asked Oct 29 '13 09:10

user2499088


People also ask

How do you navigate from one XAML page to another in WPF MVVM?

To package content for navigation, WPF provides the Page class. You can navigate from one Page to another declaratively, by using a Hyperlink, or programmatically, by using the NavigationService. WPF uses the journal to remember pages that have been navigated from and to navigate back to them.


2 Answers

Firstly, you don't need any of those toolkits/frameworks to implement MVVM. It can be as simple as this... let's assume that we have a MainViewModel, and PersonViewModel and a CompanyViewModel, each with their own related view and each extending an abstract base class BaseViewModel.

In BaseViewModel, we can add common properties and/or ICommand instances and implement the INotifyPropertyChanged interface. As they all extend the BaseViewModel class, we can have this property in the MainViewModel class that can be set to any of our view models:

public BaseViewModel ViewModel { get; set; } 

Of course, you'd be implementing the INotifyPropertyChanged interface correctly on your properties unlike this quick example. Now in App.xaml, we declare some simple DataTemplates to connect the views with the view models:

<DataTemplate DataType="{x:Type ViewModels:MainViewModel}">     <Views:MainView /> </DataTemplate> <DataTemplate DataType="{x:Type ViewModels:PersonViewModel}">     <Views:PersonView /> </DataTemplate> <DataTemplate DataType="{x:Type ViewModels:CompanyViewModel}">     <Views:CompanyView /> </DataTemplate> 

Now, wherever we use one of our BaseViewModel instances in our application, these DataTemplates will tell the framework to display the related view instead. We can display them like this:

<ContentControl Content="{Binding ViewModel}" /> 

So all we need to do now to switch to a new view is to set the ViewModel property from the MainViewModel class:

ViewModel = new PersonViewModel(); 

Finally, how do we change the views from other views? Well there are several possible ways to do this, but the easiest way is to add a Binding from the child view directly to an ICommand in the MainViewModel. I use a custom version of the RelayComand, but you can use any type you like and I'm guessing that you'll get the picture:

public ICommand DisplayPersonView {     get { return new ActionCommand(action => ViewModel = new PersonViewModel(),          canExecute => !IsViewModelOfType<Person>()); } } 

In the child view XAML:

<Button Command="{Binding DataContext.DisplayPersonView, RelativeSource=     {RelativeSource AncestorType={x:Type MainView}}, Mode=OneWay}" /> 

That's it! Enjoy.

like image 164
Sheridan Avatar answered Oct 13 '22 18:10

Sheridan


When i first started wiht MVVM I also struggled with the different MVVM-frameworks and especially the navigation part. Therefore I use this little tutorial i found, that Rachel Lim has created. It's very nice and well explained.

Have a look at it on the following link:

  • http://rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2/

Hope it helped you :)

like image 43
RooKie- Avatar answered Oct 13 '22 17:10

RooKie-