C# WPF) how to minimize a parent window after chil

2019-03-06 06:50发布

问题:

I've searched some posts on stackoverflow and some discussion concluded as there's no way to do this as it's standard behavior of WPF modal Window.

Isn't there really a solution to minimize(control) the parent Window from a modal child Window?

Anybody who hacked or knows a roundtrip ?

Your excellent ideas will be highly appreciated always.

Thank you !

回答1:

You can get the reference of your Window via application window collection. This is an example of minimizing the MainWindow which is in my example the parent window:

    private void button_Click(object sender, RoutedEventArgs e)
    {
        foreach (Window win in Application.Current.Windows.OfType<MainWindow>())
        {
            var mainWin = ((MainWindow)win);
            mainWin.WindowState = mainWin.WindowState == WindowState.Minimized ? WindowState.Normal : WindowState.Minimized;
        }
    }

You can just set the window state to minimized.

If you want to get rid of the window just use .Hide(); and to show it agian .Show();.