Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShowDialog() behind the parent window

Tags:

wpf

showdialog

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

I've found an answer here: WPF and ShowDialog() but it isn't suitable for me because I don't need an "always on top" tool window.

Thanks in advance

like image 237
Oszkar Avatar asked Mar 24 '12 21:03

Oszkar


People also ask

What is ShowDialog?

ShowDialog shows the window, disables all other windows in the application, and returns only when the window is closed. This type of window is known as a modal window. Modal windows are primarily used as dialog boxes.

What is ShowDialog C#?

ShowDialog() Shows the form as a modal dialog box. ShowDialog(IWin32Window) Shows the form as a modal dialog box with the specified owner.


3 Answers

Try setting the Owner property of the dialog. That should work.

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

Edit: I had a similar problem using this with MVVM. You can solve this by 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
    }
}
like image 72
MatthiasG Avatar answered Oct 09 '22 17:10

MatthiasG


I had this problem but as the Window was being opened from a view model I didn't have a reference to the current window. To get round it 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 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.

like image 32
GrandMasterFlush Avatar answered Oct 09 '22 17:10

GrandMasterFlush


When the parent window makes (and shows) the child window, that 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();
    }
 }

Aditionally, if you don't want an extra taskbar for all the children... then

<Window x:Class="ChildWindow"           
        ShowInTaskbar="False" >
</Window>
like image 25
00jt Avatar answered Oct 09 '22 19:10

00jt