1

How to pass a parameter to another page and read it in WPF? I read on the internet that this could be done using the URL as the following:

NavigationService n = NavigationService.GetNavigationService(this);
n.Navigate(new Uri("Test.xaml?param=true", UriKind.Relative));

But I'm not able to read the parameter value in the Test.xaml page.

I cannot instantiate a new instance from the page and pass it by the constructor because I had a problem before the round-trip was to navigate using the page path.

Homam
  • 23,263
  • 32
  • 111
  • 187

2 Answers2

3

read this:

http://paulstovell.com/blog/wpf-navigation

Although it's not obvious, you can pass query string data to a page, and extract it from the path. For example, your hyperlink could pass a value in the URI:

<TextBlock>
    <Hyperlink NavigateUri="Page2.xaml?Message=Hello">Go to page 2</Hyperlink>
</TextBlock>

When the page is loaded, it can extract the parameters via NavigationService.CurrentSource, which returns a Uri object. It can then examine the Uri to pull apart the values. However, I strongly recommend against this approach except in the most dire of circumstances.

A much better approach involves using the overload for NavigationService.Navigate that takes an object for the parameter. You can initialize the object yourself, for example:

Customer selectedCustomer = (Customer)listBox.SelectedItem;
this.NavigationService.Navigate(new CustomerDetailsPage(selectedCustomer));

This assumes the page constructor receives a Customer object as a parameter. This allows you to pass much richer information between pages, and without having to parse strings.

Afshin
  • 1,222
  • 11
  • 29
0

You can use the overload Navigate(object,object) to pass data to the other view.

Call it like so

NavigationService n = NavigationService.GetNavigationService(this);
n.Navigate(new Uri("Test.xaml", UriKind.Relative), true);

And in your view you can extract the parameter that is passed by the Navigation.

void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
    bool test = (bool) e.ExtraData;
}
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • 3
    Can you expand on how do you register for the NavigationService_LoadCompleted event inside the Text.xaml page? Is it inside the Test.xaml constructor? Or inside Text.xaml's Loaded event? Or some other way? In the first case I get a NullReferenceException (NavigationService is null), in the other the NavigationService_LoadCompleted never gets called. – krdx May 16 '14 at 22:07
  • I don't see how this can work. The LoadCompleted event occurs in the class that called the Navigate method, not in the View that is navigated to. Happy to be wrong because it would be damned convenient. – Steve Apr 26 '19 at 14:37
  • According to this page, it looks like you should override the `OnNavigatedTo(NavigationEventArgs e)` method instead of handling the LoadCompleted event. However, I haven't tested this. http://developer.intersoftsolutions.com/display/ClientUI/How-to%3A+Navigate+to+a+Page+with+Extra+Data – Matt Mar 04 '20 at 04:27
  • For those who fall here like me... here is the answer on how to handle the `NavigationService_LoadCompleted` event: https://stackoverflow.com/a/38260591/9203951 – dwpessoa Mar 03 '23 at 11:38