Search code examples
c#wpftopmost

Make all wpf project windows topmost programmatically


It is possible to make individual windows topmost in the Visual Studio designer or with code, however is there a way to make every window topmost at once with code?

The issue I have is that just making the main parent window topmost hides any other windows, so obviously I need to make them all topmost, or am I wrong?


Solution

  • Firstly the owner of the window has to be specified when the instance is created.

    The following example shows how the new window has the owner set first before it is shown. MyWindow _MyWindow = new MyWindow() { Owner = Application.Current.MainWindow };

    NOTE: The window being set as the owner has to be shown already otherwise you might get a XamlParseException or InvalidOperationException.

    Secondly you just need to use _MyWindow.ShowDialog() to show the window. This prevents the owner window from moving on top of the owned window even if it is set to topmost after the child window is shown.