Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change content of WPF Window

Tags:

c#

wpf

When I click on a Button on my navigation bar, a new Window opens. For example:

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

    }

Now, I don´t want to show it in a new window. Instead of that, I want it to be shown at the same window. I tried with the following:

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

    }

but it failed. What I should say is, that I´ve many of different windows now and want them all to be shown at one window - each by clicking a different button. How can I manage this?

like image 664
user896692 Avatar asked Mar 23 '12 14:03

user896692


2 Answers

What you have to do is to use a UserControl rather than a Window for your replaceable content. Add a new UserControl for each possible content you want to show in this.Content to your project. Add the content you had previously in the second window to the appropriate UserControl. After that you can simply instantiate the 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 content of your previous Finanz window. Now you simply write:

this.Content = new ctlFinanz(login);

That's all :-)

like image 140
Torben Schramme Avatar answered Oct 30 '22 08:10

Torben Schramme


I had the same problem and decided to create a seperate usercontrol for every window, put them in my wpf and set the visibility of all windows except the active one to Collapsed.

like image 45
hcb Avatar answered Oct 30 '22 07:10

hcb