views:

188

answers:

2

I've seen this done inside of an event handler directly behind the .xaml file however it doesn't seem like this would follow the MVVM pattern: MainApplication.mainFrame.Navigate(new HomePage());. Is there a better way for handling navigation with the MVVM pattern perhaps in the ViewModel? or in the XAML?

A: 

If you are looking for showing different UserControls based on the context of your data, then just understand the following simple DataBinding and DataTemplate concept and expand on it. Imagine you got a Property called CurrentViewModel which binds to the Content of a ContentControl inside your Window

 <Window ... 
    <ContentControl Content="{Binding CurrentViewModel}" />
 </Window>

Now imagine that you got ViewModel classes ClassA and ClassB, so appropriately set the instances to CurrentViewModel and define global DataTemplates (Views) for your classes

<DataTemplate DataType="{x:Type vm:ClassA}">
    <local:UserControlForA../>
</DataTemplate>


<DataTemplate DataType="{x:Type vm:ClassB}">
    <local:UserControlForB../>
</DataTemplate>

Now the View is automatically controlled from ViewModel logic and WPF will take care of showing the UserControl by Datatemplate.

If you are not familiar with MVVM better use this article. http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Jobi Joy
That is exactly what I was doing, but I couldn't figure out this: once I've got a userControl in the view, how would I accomplish having that UserControl change to another control and then come back to the previous one? Pages seemed natural for that, but if I can accomplish it this way as well, that would work.
Mike
when your View is totally build up with Databinding then when 'coming back' is merely setting the previous instance of the ViewModel. So I am not seeing anything unusual in your requirement, and what MVVM is for.
Jobi Joy
Yes, I think I understand that. However, I think we are on different pages. I understand how to change the UserControl being rendered in a tabControl as shown in the Microsoft guide. The problem I'm having would be this: Say there exist a menuOption or button inside of the current UserControl that is in the "workspace". How would I tell that button to navigate me to a new userControl in the same tab?
Mike
I think I am on the same page as per your requirement. Did you try making proper ViewModels with ICommands in it? Then the button will have Command and Command action will set the CurrentViewModel and thus the View will change. If you can simplify your situation to a sample code and add in the question, we can help appropriately.
Jobi Joy
Ok, I'll get some sample code up tomorrow. Thanks for your help.
Mike
A: 

I think what you're trying to do would be more simply done if you had the navigation in another class. See below

public class FirstViewModel
{

}

public class SecondViewModel
{

}

public class NavigateViewModel
{
  public ViewModelBase CurrentVieModel {get;set;}

  public bool CanNavigate
  {
     get { return true;//Or Add some custom logic here determine if you can navigate}
  }
  public void Navigate()
  {
     //Just some arbitrary code
     if(CurrentViewModel is FirstViewModel)
       CurrentViewModel = new SecondViewModel();
  }

}

Now just 1) bind the Page Content to CurrentViewModel 2) Wrap the Navigate Method in an ICommand and you're set

Might not meet your needs, hope it helps

Jose