CStruggle
CStruggle

Reputation: 173

How to access XAML objects in a different page

I have a Windows 10 app that has a MainPage.xaml file. In that file I hold the basic menu (hambuger menu) and there is this Frame object that allows me to load new pages when a selection on the menu changes: <Frame Name="MyFrame"></Frame>

So when the selection in the ListBox changes, there is this event handler that is pretty straightforward: MyFrame.Navigate(typeof(WeekPage));

This line of code loads the Frame with another XAML page. Now my question is, inside of the WeekPage.xaml.cs, can I control where the MyFrame object navigates?

In other words, I want to control XAML objects and their properties (buttons, textblocks, listbox) on one XAML file (MainPage.xaml) from the code-behind of another XAML file (WeekPage.xaml.cs).

I have already tried the following: <Frame Name="MyFrame" x:FieldModifier="Public"></Frame> which didn't seem to help at all and setting up internal static NAMESPACE.MainPage tried; on MainPage.xaml.cs with MainPage.tried.MyFrame.Navigate(typeof(StoryPage)); on WeekPage.xaml.cs(I got that idea from here) but I get a System.NullReferenceException when I run the program.

Another example to narrow down on what I am looking for There is a MainPage.xaml that contains the basic layout for the app (hamburger menu), depending on the item that the user clicks on in the 'ListBox' in for the hamburger menu a new frame is loaded, for example: WeekPage.xaml. WeekPage.xaml has a GridView that, when clicked, should replace the current frame with a new one and clear out any selections on the ListBox in MainPage.xaml. See code below:

private void GridView_ItemClick(object sender, ItemClickEventArgs e)
{
    // Replacing current frame with new frame
    Frame.Navigate(typeof(StoryPage));
    // TODO: I need to clear out any selection in the ListBox item in MainPage.xaml
}

Upvotes: 1

Views: 4049

Answers (1)

ViVi
ViVi

Reputation: 4464

If you’re not aware of the MVVM approach, please read more on it. In MVVM approach, each view will be bound to a view model. Ie The data in view will be bound to view model properties. For eg : Say you have a text box, you bind the Text property of it to a string property in its corresponding view model.

In your scenario we can have a MainView consisting of 2 subviews.

MainWindow.xaml - MainWindowViewModel
SubView1.xaml - SubView1VM
SubView2.xaml - SubView2VM

MainView will consist of 2 sub views in xaml and also 2 view models of subviews in main view model. You can also have commands within each sub viewmodel corresponding to button clicks or any such events.

You could handle these commands in the MainWindowViewModel. Suppose in your view 2 you have a button that transits to view 1 as well as change all the textbox values in view 1. Handle the button command in MainWindowViewModel and change the SubView1VM properties from within the MainVM.

Hope you got the idea of MVVM. Your requirement is easily achievable using MVVM. MVVM is more coder friendly and helps to separate the view data from model.

For anyone looking to get started on MVVM
https://mva.microsoft.com/en-US/training-courses/building-blocks-universal-windows-platform-16064?l=5V1wM0kDC_2606218949

Upvotes: 1

Related Questions