Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - window.ShowDialog inside window.Loaded

In WPF application, I would like to show window b as a dialog inside window a when window a is loaded. I do this with the following pseudocode:

window a.Loaded += WindowALoaded();
WindowALoaded
{
  window b.ShowDialog();
}

This works. However, it displays window b, and window a does not get displayed until I close window b. I would like to display window a completely, and then window b. How would I accomplish this?

like image 722
laconicdev Avatar asked Nov 17 '25 21:11

laconicdev


2 Answers

You may use the Activated event rather then the Loaded

<Window x:Class="WpfApplication.WindowA"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowA" Height="300" Width="300" 
        Activated="Window_Activated_1"  >
(...)
</Window>

On your code behind:

 private void Window_Activated_1(object sender, EventArgs e)
 {       
    WindowB windowB = new WindowB();
    windowB.ShowDialog();
  }

Also, if don´t wanna use the XAML, this also works perfectly.

    public WindowA()
    {    
        this.Activated += Window_Activated_1;
    }
like image 99
user1959018 Avatar answered Nov 19 '25 13:11

user1959018


Its because in the load event of WindowA, it does a ShowDialog() of WindowB which then haults all code in WindowA until WindowB is closed. If you do just WindowB.Show(), you should probably see WindowA get loaded. You might need to mess with the Window.Focus() and/or the Window.TopMost properties, depending on how you want to windows to be displaying on top of each other.

There also is a Window.ContentRendered event instead of the Window.Loaded event which might help in your solution

like image 39
devman Avatar answered Nov 19 '25 13:11

devman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!