Talen Kylon Talen Kylon - 2 months ago 9
C# Question

How to get child window to minimize and maximize with the main window

My application runs queries and while they run, I disable the main application and display a progress bar screen. I would like the two screens to behave as a single entity. What I mean by that is if the user minimizes the main application during execution of the query, then the progress screen also gets minimized. If they maximize or bring the main application back up, the progress screen comes back up as well (and is in the front).

I tried accomplishing this by using the _StateChanged event and manipulating the progress screen inside of there but it's not working. The WriteLines fire as they should, but there's no changes in the progress screen (ie. the progress screen does not get maximized when I maximize the main app).

private void mainWindow_StateChanged(object sender, EventArgs e)
{
Console.WriteLine("State Changed");

// if the progress screen is not null
if (ql != null)
{
// if main application has been minimized
if (this.WindowState == WindowState.Minimized)
{
Console.WriteLine("State Changed to Minimized");
ql.WindowState = WindowState.Minimized; // minimize progress screen
}
// if main application has been set to normal
if (this.WindowState == WindowState.Normal)
{
Console.WriteLine("State Changed to Normal");
ql.WindowState = WindowState.Normal; // set progress screen state to normal
ql.Focus(); // bring progress screen to focus
}

// if main application has been maximized
if (this.WindowState == WindowState.Maximized)
{
Console.WriteLine("State Changed to Maximized");
ql.WindowState = WindowState.Maximized; // set progress screen state to maximized
ql.Focus(); // bring progress screen to focus
}
}
}


I was curious if there were properties in the .xaml that I should be leveraging instead of trying to accomplish this in the code. If there isn't, what am I missing with my code?

Answer

If ql is a Window then you can set it's Owner to the mainWindow like this:

ql.Owner = mainWindow;

You need to do this only once before showing the ql window. And you won't need to do anything in StateChanged about ql anymore.

Comments