ShowDialog () behind the parent window

I am using ShowDialog() with WindowStyle = WindowStyle.SingleBorderWindow; to open a modal window in my WPF application (MVVM), but it allows you to go to the parent window using the Windows taskbar (Windows 7).

I found the answer here: WPF and ShowDialog () , but it is not suitable for me, because I do not need the "always on top" tool window.

Thanks in advance

+6
source share
6 answers

Try setting the Owner property in the dialog box. That should work.

 Window dialog = new Window(); dialog.Owner = mainWindow; dialog.ShowDialog(); 

Edit: I had a similar problem using MVVM. You can solve this using delegates.

 public class MainWindowViewModel { public delegate void ShowDialogDelegate(string message); public ShowDialogDelegate ShowDialogCallback; public void Action() { // here you want to show the dialog ShowDialogDelegate callback = ShowDialogCallback; if(callback != null) { callback("Message"); } } } public class MainWindow { public MainWindow() { // initialize the ViewModel MainWindowViewModel viewModel = new MainWindowViewModel(); viewModel.ShowDialogCallback += ShowDialog; DataContext = viewModel; } private void ShowDialog(string message) { // show the dialog } } 
+10
source

I had this problem, but since the window was opening from the view model, I did not have a link to the current window. To get around this, I used this code:

 var myWindow = new MyWindowType(); myWindow.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive); 

You can use: myWindow.Owner = Application.Current.MainWindow;

However, this method causes problems if you have three windows open:

 MainWindow | -----> ChildWindow1 | -----> ChildWindow2 

Then set ChildWindow2.Owner = Application.Current.MainWindow to establish that the owner of the window is its grandfather window, not the parent window.

+2
source

When a parent window creates (and shows) a child window, this is where you need to set the owner.

 public partial class MainWindow : Window { private void openChild() { ChildWindow child = new ChildWindow (); child.Owner = this; // "this" is the parent child.ShowDialog(); } } 

In addition, if you do not need an additional taskbar for all children ... then

 <Window x:Class="ChildWindow" ShowInTaskbar="False" > </Window> 
+1
source

Add "ShowInTaskbar" and set it to false.

0
source

Even if this message is a little outdated, I hope that everything is in order, I will send my decision. All the above results are known to me and definitely do not give the desired result.

I do this for other googlers :)

Let's say that f2 is your window that you want to display on top of f1:

 f2.Owner = Window.GetWindow(this); f2.ShowDialog(); 

What, I promise it won’t disappear!

NTN Guy

0
source

Most of the reason for the MVVM pattern is because your interaction logic can be verified by the module. For this reason, you should never open a window directly from the ViewModel, otherwise you will have dialogs in the middle of your unit tests.

Instead, you should raise the event that the View will handle and open a dialog for you. For example, see this article on interaction requests: https://msdn.microsoft.com/en-us/library/gg405494(v=pandp.40).aspx#sec12

0
source

Source: https://habr.com/ru/post/911581/


All Articles