ford
ford

Reputation: 1887

How do I send messages to a Page within a Frame Control?

I have a window with a primary navigation column (ListBox) and a detail view for the currently selected item. When I select an item in the ListBox, the detail view will display a set of controls (editable text fields, etc) that are relevant to the selected item.

So far I have chosen to use a Frame which can display different Pages by changing Frame.Source . Additionally, I would like the Page to load information from the selected listbox item when it is first clicked, and to save the same information displayed in the detail view back to the same item when the Listbox's selected item changes.

Since the Pages and the MainWindow are in different XAML files they cannot inherently see each other. How would I pass such a message? I'm thinking of something conceptually similar to a JSON call where you can pass a series of parameters to another page and receive a response.

Thanks to the response below, I found This guide about NavigationServices. This code example was especially relevant:

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

What I needed to do was call the new page's constructor with the object I wanted to pass as an argument. Since the object is passed by reference, changes made to this object will automatically be reflected in other areas of the application where the object is present.

Upvotes: 3

Views: 110

Answers (1)

Vinit Sankhe
Vinit Sankhe

Reputation: 19885

I think if Page1 hosts frame say MyFrame that navigates to Page2 then in the NavigationService.Navigated event from frame....

     MyFrame.NavigationService.Navigated +=
         (sender, e) =>
            {
                  var nextPageContent = MyFrame.NavigationService.Content;
                  //// this is the root of the content of Page2.
            }

nextPageContent can supply its own DataContext etc...

Upvotes: 2

Related Questions