Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Visibility="Hidden" on WPF Window never shows the window again

Tags:

c#

vb.net

wpf

xaml

I have set the Visibility property of the main window to Hidden and added the following in Window_Loaded:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.Visibility = System.Windows.Visibility.Visible;
    }

But it doesn't show up the Window. Any specific reason for this?

like image 608
Elmo Avatar asked Jan 08 '12 20:01

Elmo


2 Answers

The window is not loaded until it is shown, as per your code it will not be shown until it is loaded. Obivously this cannot work like that, right?

like image 138
H.B. Avatar answered Oct 31 '22 11:10

H.B.


I was having an issue with this as well and it seems changing the visibility alone on a main window doesn't work as H.B. pointed out. For my case, I wanted to not show the window until it was completely loaded and was able to achieve that by using the property I've linked to here, along with the Show() and Hide() functions on the Window object. System.Windows.Window.ShowActivated

  1. When initializing the window object don't set visibility to hidden, instead follow the next steps
  2. Set the ShowActivated property to false this.ShowActivated = false;
  3. Call the Hide() function on the window object this.Hide();
  4. On your window loaded function from your original example call this.Show();

It is also possible in some WPF apps for the this reference not to work as expected, however if this is the case go to the XAML and find the name property of the window. You should be able to ref the window from the code via that name. Ex.

<Window x:Name="MainWindow">
//Code Behind Below
MainWindow.ShowActivated = false;
like image 43
Jordan H Avatar answered Oct 31 '22 13:10

Jordan H