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

11,297

Solution 1

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?

Solution 2

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;
Share:
11,297

Related videos on Youtube

Elmo
Author by

Elmo

Updated on June 04, 2022

Comments

  • Elmo
    Elmo almost 2 years

    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?

  • Adi Lester
    Adi Lester over 12 years
    @Programmer I don't really see the point of doing something like this anyway, but if you'd set the window's initial visibility to Collapsed, the Loaded event will get fired.