1

I have 2 xaml files L1.xaml and L2.xaml At first L1 will open. It has a button which when pressed opens L2. But I want L2 to open in the same window as L1.

More precisely, when the button is clicked, L1 should terminate and L2 should start without the main window disappearing even for a second.

Sam
  • 7,252
  • 16
  • 46
  • 65
sketch_TF2
  • 61
  • 1
  • 9

2 Answers2

1

There are many ways to do that. 1.- You can create a navigation Page and your L1.xaml and L2.xaml they need to be a Page. Here is a good article showing this. http://paulstovell.com/blog/wpf-navigation

2.- You can add in your MainPage a Frame control,with this control you can navigate between pages( again your L1 and L2 must be Pages.( it's almost the same of the first point but the difference you can use the Frame control inside of other control like UserControl.

3.- You can use a ContentControl or ContentPresenter as your container these controls have the property Content when the user click some button to show L1 or L2 xaml (doesn't matter if they are an UserControl or Page) you just need to create a new instance of your view, for example: MyContentControl.Content = new MyView1();

obviously this technique is very simple and you will need to check what of these options are enough for your requirements.

Best Regards!

RicardoPons
  • 1,323
  • 9
  • 14
  • Thank you for the explanation but I can't make it as a page.... since there are many Key Down events and focus is set to Window. In UserControl, idk why but the Key Down events didn't work. In case of Page, It's a game so I don't want browser window with back n forward to open up. – sketch_TF2 Oct 17 '15 at 16:55
  • you should use the third option that I explained. In your window just put a content control and you will need to change the views setting the property Content with a new instance of your usercontrols if you want to navigate from L1 to L2 and the button is inside of the L1 you can access to the parent of your UserControl (it should be the ContentControl) and change the property Content with your new View : Example. var parent = L1.Parent as ContentControl; parent.Content= new myNewView(); – RicardoPons Oct 17 '15 at 19:05
0

What you need to do is create a navigationpage and then modify L1 and L2 to be pages. That will allow you to swap the pages at runtime and use the event to assign the page. Please refer to this stack overflow page for different ways to implement Window vs Page vs UserControl for WPF navigation?

Community
  • 1
  • 1
SteckDEV
  • 489
  • 4
  • 13