Andrew Arnold Andrew Arnold - 19 days ago 8
C# Question

Keeping dialogs on top of window, but not on top of everything

In my WPF application I have a lot of custom dialog boxes that pop open so the user can do various things with

someDialogClass.ShowDialog()
. To make sure that the dialog stays on top of the window that called it, I add
Topmost="True"
to the
Window
tag of the dialog's XAML file. This works, but the dialog is shown over every window open—even other applications. This is really annoying. So is there a way to have the dialog forced to always be on top of its parent, but not necessarily on top of other applications?

Here is a simplified version of the
Window
tag of the dialogs I have (omitting all the
xmlns
stuff):

<Window
mc:Ignorable="d"
ShowInTaskbar="False"
Topmost="True"
WindowStartupLocation="CenterOwner"
ResizeMode="NoResize"
SizeToContent="WidthAndHeight"
WindowStyle="ToolWindow">

bic bic
Answer

You need to set the Owner of the Dialog/Window and it will then be on top of only that window.

For example:

var loginForm = new LoginForm();
loginForm.Owner = Application.Current.MainWindow;
var success = loginForm.ShowDialog();

Do not set the TopMost property on the Window otherwise it will be on top of every window.

Comments