Changing the contents of a WPF window

When I click a button on my navigation bar, a new window opens. For instance:

private void btFinanzen_Click(object sender, RoutedEventArgs e) { Finanz mw = new Finanz(login); mw.Show(); } 

Now I do not want to show this in a new window. Instead, I want it to appear in the same window. I tried the following:

  private void btFinanzen_Click(object sender, RoutedEventArgs e) { Finanz mw = new Finanz(login); //mw.Show(); this.Content = Finanz f; } 

but it failed. I have to say that now I have many different windows, and you want all of them to be displayed in one window - each, by clicking another button. How can i do this?

+6
source share
3 answers

What you need to do is use UserControl and not Window for your plugin content. Add a new UserControl for every possible content that you want to show in this. Content of your project. Add the content that you had earlier in the second window to the appropriate UserControl. After that, you can simply create a new control in your code and assign it to your content area of ​​your main window.

For example, you create a UserControl ctlFinanz with the contents of your previous Finanz window. Now you just write:

 this.Content = new ctlFinanz(login); 

What all: -)

+6
source

I had the same problem and I decided to create a separate user control for each window, put it in my wpf and set the visibility of all windows except the active one for Collapsed.

+1
source

For me, this sounds like a place to use a page frame. Instead of creating many windows, make a single window with a border, and then place the content on the pages. Then all you have to do is change the frame. Still on another page when you press the navigation button.

0
source

Source: https://habr.com/ru/post/911504/


All Articles