I am using Window.ShowDialog() to open a modal window in my WPF (MVVM) application, but it lets me navigate to other windows using the Windows taskbar (Windows
I ended up using a combination of a couple of the answers here. The accepted answer was useful at first but as other people on here have pointed out setting Topmost = true means that the window is always above any other applications running. My solution was this:
var myWindow = new MyWindowType();
myWindow.Owner = Application.Current.Windows.OfType().SingleOrDefault(x => x.IsActive);
I initially used:
myWindow.Owner = Application.Current.MainWindow;
However, this method causes problems if you have three windows open like this:
MainWindow
|
-----> ChildWindow1
|
-----> ChildWindow2
Then setting ChildWindow2.Owner = Application.Current.MainWindow will set the owner of the window to be its grandparent window, not parent window.
To speed things up, I've added it as a code snippet in Visual Studio. If you add the following to Tools --> Code Snippet Manager --> My Code Snippets:
MVVM Set owner of page to be current active window
owner
().SingleOrDefault(x => x.IsActive);]]>
Typing 'owner' and double-tapping the tab key will add the 'Application.CurrentWindows...' part in for you automatically.