Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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">
like image 583
Andrew Avatar asked Jun 03 '11 22:06

Andrew


1 Answers

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.

like image 112
bic Avatar answered Oct 28 '22 12:10

bic