Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Frame Source Wpf

Tags:

c#

wpf

frame

How can I possibly change the source of a frame from other page when I click a button in an active page.

What I'm doing is In page1.xaml, I have a frame with a source displaying page2.xaml.Once I click a button in page2.xaml, I want to update the source of frame in page1.xaml to page3.xaml and the frame should display page3.xaml instead of page2.xaml.

As of now, I tried using

page1 pg1 = new page1();
pg1.frame.source = new Uri("page3.xaml",UriKind.Relative);

But it didn't display page3.xaml in the frame in page1.xaml.

I also tried

page1 pg1 = new page1();
pg1.frame.Navigate(new Uri("page3.xaml",UriKind.Relative));

but didn't work as well, page2.xaml remains the display of the frame.

like image 437
Kuriyama Mirai Avatar asked Feb 10 '23 07:02

Kuriyama Mirai


1 Answers

First of all, it is unusual to have frame inside page. Usually you have frame inside MainWindow or Usercontrol, because Page is meant to be hosted inside frame.

Second, you can't just create new Page1 and use it's frame. This newly created Page1 exist only in memory and it is another instance that the one displayed.

Because it would be quite difficult to find the Frame from pages, there is NavigationService property in Page class.

NavigationService.Navigate(new Uri("page3.xaml",UriKind.Relative));
like image 129
Liero Avatar answered Feb 12 '23 10:02

Liero