Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing a hidden WPF window

Tags:

c#

wpf

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?

like image 547
Mahmoud Samy Avatar asked Aug 01 '13 12:08

Mahmoud Samy


People also ask

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

Is WPF discontinued?

“WPF would be dead in 2022 because Microsoft doesn't need to be promoting non-mobile and non-cloud technology. But WPF might be alive in that sense if it's the best solution for fulfilling specific customer needs today. Therefore, having a hefty desktop application needs to run on Windows 7 PCs with IE 8.

What is WPF visibility?

In WPF UIElement. Visibility has 3 states; Visible/Hidden/Collapsed. If hidden, the control will still affect the layout of surrounding controls, elements that have a Visibility value of Collapsed do not occupy any layout space. You can switch between visible and hidden or collapsed.

What is the difference between WPF window and WPF page?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.


1 Answers

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

like image 86
Anthony Russell Avatar answered Sep 30 '22 17:09

Anthony Russell