locked
Navigate to new XAML page - MVVM RRS feed

  • Question

  • Hello all

    I have a simple question: Is it possible to navigate to page2.xaml, when pressing a button in page1.xaml, that is binded to a command in page1_Viewmodel.cs? I am guessing using Uri mapping?

    If not... then what is the alternative? How can one navigate to different pages from the ViewModel?

    Thank you so much in advance. 

    Friday, August 20, 2010 6:19 AM

Answers

  • Hi!

    You can make an instance of your second page in the eventhandler and then just set the content of the current page to be the second page.

    Page2 page2 = new Page2();
    this.Content = page2


    Hope this helps!

     

    //K 

    Friday, August 20, 2010 8:04 AM
  • Hi again!

    You can extend the constructor of the new page to take for example a userID and then just pass it in when invoking the new page.

    //In the calling page
    Page2 page2 = new Page2(userID)
    
    //In the new page
    private string userID = String.Empty;
    
    public Page2(string userID)
    {
        InitializeComponent;
        
        this.userID = userID;
    }

    If it is an object you would like to pass I'd prefer to use the DataContext-property.

    Page2 page2 = new Page2();
    page2.DataContext = yourObject;
    


    This allows you to access the property in the page2 behind code.

    Hope this helps!!

    //K

     

    Monday, August 23, 2010 2:25 AM

All replies

  • Hi!

    You can make an instance of your second page in the eventhandler and then just set the content of the current page to be the second page.

    Page2 page2 = new Page2();
    this.Content = page2


    Hope this helps!

     

    //K 

    Friday, August 20, 2010 8:04 AM
  • That worked, thanks!

    All though I would say that it's slightly un-MVVM'ish (having eventhandlers in the code-behind), this solution works for me. Thank you!! :) 

    Friday, August 20, 2010 9:13 AM
  • Just an additional question: How would you best do to pass data from one page no another? Say, I want to pass the userID... how can you make an property that's available to all pages?

    Friday, August 20, 2010 2:38 PM
  • I would say that it's slightly un-MVVM'ish (having eventhandlers in the code-behind)

    You should be able to make a Behavior to do this. For example in this behavior we are creating a child control:

    Using the “Hisowa Simple PopUp Behavior” in a DataGrid

    In this one we are creating a File Dialog Window:

    Silverlight Open File Dialog Behavior (MVVM)

     

    Sunday, August 22, 2010 9:11 AM
  • Hi again!

    You can extend the constructor of the new page to take for example a userID and then just pass it in when invoking the new page.

    //In the calling page
    Page2 page2 = new Page2(userID)
    
    //In the new page
    private string userID = String.Empty;
    
    public Page2(string userID)
    {
        InitializeComponent;
        
        this.userID = userID;
    }

    If it is an object you would like to pass I'd prefer to use the DataContext-property.

    Page2 page2 = new Page2();
    page2.DataContext = yourObject;
    


    This allows you to access the property in the page2 behind code.

    Hope this helps!!

    //K

     

    Monday, August 23, 2010 2:25 AM