Mahmoud Samy
Mahmoud Samy

Reputation: 2852

Showing a hidden WPF window

In a WPF window I want to hide it, show another window using ShowDialog then unhide the first window.


When I do that:

this.Hide();
var window2 = new Window2();
window2.ShowDialog();
this.Show();

The first window open as a blank and empty window.

What's wrong in this technique?


When I do that:

var window2 = new Window2();
Visibility = Visibility.Collapsed;
window2.ShowDialog();
Visibility = Visibility.Visible;

The first window exits then the application.

What's wrong in this technique too?

Upvotes: 11

Views: 41804

Answers (6)

Eduard Sionov
Eduard Sionov

Reputation: 31

This code solved my problem

var window = new Window()
{
    WindowStyle = WindowStyle.Hidden,
    Height = 0;
    Width = 0; 
};
window.Show();

Upvotes: 0

DotNetRussell
DotNetRussell

Reputation: 9867

Do this instead:

this.Visibility = Visibility.Collapsed;    
...    
this.Visibility = Visibility.Visible;

Also, I saw your comment above that this doesn't work. However, I started a new WPF project, did this, built and ran it. It works.

Note that there are no errors.

enter image description here

Upvotes: 17

uTILLIty
uTILLIty

Reputation: 475

Check your Application.Current.ShutdownMode. Closing the window you assign to Application.Current.MainWindow will cause the application to shutdown, if Application.Current.ShutdownMode is set to OnMainWindowClose. In your case I would set it to OnExplicitShutdown. You can see the default values being set in app.xaml of your WPF application project.

Upvotes: 2

Randy Beers
Randy Beers

Reputation: 31

foreach (Window window in App.Current.Windows)

        {
            if (!window.IsActive)
            {
                window.Show();
            }
        }

Works fine for me

Upvotes: 3

Artiom
Artiom

Reputation: 7847

In my case next code worked out:

new HiddenWindow
    {
       WindowStyle = WindowStyle.None,
       AllowsTransparency = true,
       Opacity = 0.0
     }.Show();

Upvotes: 3

yo chauhan
yo chauhan

Reputation: 12315

 Window2 window2  = new Window2();
        this.Visibility = Visibility.Collapsed;
        window2.ShowDialog();
        this.Visibility = Visibility.Visible;

Upvotes: 3

Related Questions