WPF ShowDialog

There is an issue with ShowDialog for Windows not always obeying Modal Dialogs rules.

So I have created a static class which all ShowDialog calls will be redirected.

public static bool ShowDialog(Window dialog,Window owner)
    if (owner != null)
        WindowInteropHelper helper = new WindowInteropHelper(dialog);
        helper.Owner = new WindowInteropHelper(owner).Handle;
    bool? dialogResult = dialog.ShowDialog();
    return dialogResult.HasValue ? dialogResult.Value : false;


I’ve made some corrections so this is the new code

public static bool ShowDialog(Windowdialog, Windowowner)
   WindowInteropHelper helper = newWindowInteropHelper(dialog);
   if(owner != null)
      helper.Owner = newWindowInteropHelper(owner).Handle;
      helper.Owner = GetActiveWindowClass.GetActiveWindow();
   bool? dialogResult = dialog.ShowDialog();
   returndialogResult.HasValue ? dialogResult.Value : false;

where GetActiveWindowClass.GetActiveWindow is a interop wrapper

public static class GetActiveWindowClass
    public static extern IntPtr GetActiveWindow();




  1. I miss something here. What problem does this Solve? I dont want the Modal Dialog to be shown when I press Alt+Tab, means when I switch to a different window.

  2. I can’t understand the problem with Alt+tab. I haven’t seen any problems. The reality is that I’m not in the project that is using the above code, so If I have time I’ll try to test the Alt+Tab.

    The reason this post was made, is because reagardles the Alt+Tab WPF allowed you to click on windows that you shouldn’t be able, since a Modal window was openned.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s