How do I determine if a WPF window is modal?

What's the easiest way to figure out if a window is opened modally or not?

CLARIFICATION:

I open a window calling

myWindow.ShowDialog();

I have a footer with an "OK" & "Cancel" button that I only want to show if the window is opened modally. Now I realize I can set a property by doing this:

myWindow.IsModal = true;
myWindow.ShowDialog();

But I want the window itself to make that determination. I want to check in the Loaded event of the window whether or not it is modal.

UPDATE

The IsModal property doesn't actually exist in a WPF window. It's a property that I have created. ShowDialog() blocks the current thread.

I'm guessing I can determine if the Window is opened via ShowDialog() by checking if the current thread is blocked. How would I go about doing that?

From http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c95f1acb-5dee-4670-b779-b07b06afafff/

"System.Windows.Interop.ComponentDispatcher.IsThreadModal can tell you if the calling thread is currently running a modal hwnd."

https://social.msdn.microsoft.com/Forums/vstudio/e, What's the easiest way to figure out if a window is opened modally or not? CLARIFIACTION: I open a window calling myWindow.ShowDialog(); I have a footer  I want to check in the "Loaded" event of the window whether or not it is modal. UPDATE The "IsModal" property doesn't actually exist in a WPF window. It's a property that I have created. ShowDialog() blocks the current thread. I'm guessing I can determine if the Window is opened via ShowDialog() by checking if the current thread is blocked.

There's a private field _showingAsDialog whenever a WPF Window is a modal dialog. You could get that value via reflection and incorporate it in an extension method:

public static bool IsModal(this Window window)
{
    return (bool)typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(window);
}

The value is set to true when the window is shown as modal (ShowDialog) and set to false once the window closes.

How do I determine if a WPF window is modal?, I want to check in the "Loaded" event of the window whether or not it is modal. UPDATE The "IsModal" property doesn't actually exist in a WPF window. So the window of the second WPF application will be disabled and on top of that window from first WPF application will be shown. Required behavior is same as showing a window in modal mode in a single WPF application.

Okay, since my last idea got voted down, I proved it. this works - and I tested it in a new WPF application, so I know it works:

In my main Window's (Window1) Loaded event, I did:

Dim frm As New Window2
frm.ShowDialog()

In my Window2 I shadowed the ShowDialog() method

Private _IsModal As Boolean = False 'This will be changed in the IsModal method

Public Property IsModal() As Boolean
  Get
    Return _IsModal
  End Get
  Set(ByVal value As Boolean)
    _IsModal = value
  End Set
End Property

Public Shadows Sub ShowDialog()
  IsModal = True
  MyBase.ShowDialog()
End Sub

In my Loaded event, I then fired off a message box to make sure that the IsModal property got changed from False to True and it gives me True, so I know IsModal was set. MyBase.ShowDialog() then forces the base class to be loaded as Modal. Shadows allows us to override the default behaviour even though the ShowDialog() method wasn't declared as overridable.

While it doesn't "self determine" it doesn't require you to pass in any boolean value from outside, and doesn't require you to set the IsModal from outside, it sets it inside itself, it's accessible from outside if you so chose to use it that way. It sets the value only if it us loaded using the ShowDialog() method and not if you use the Show() method. I doubt you'll find a much simpler method of doing this.

How do I determine if a WPF window is modal? - .net, I suggest using the following procedure to determine whether there is a modal WPF dialog: Get the main window object. Get the window object of the dialog you want to check. Try to activate the main window. Find out whether the OnUnexpectedWindow event is raised. One solution could be to make a semi transparent WPF window that covers the entire screen (actually you should say: all screens) and show the modal window using Window.ShowDialog() with the owner being that window. All you need to do then is prevent task switching (which is not an easy task), but maybe doing that is enough?

Here is a converter fragment which is useful for hiding elements when the window is invoked by the ShowDialog method:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Window window = value as Window;
        if (window != null)
        {               
            var showingAsDialogFieldInfo = typeof(System.Windows.Window).GetField("_showingAsDialog",
                BindingFlags.NonPublic | BindingFlags.Instance);
            if ((bool) showingAsDialogFieldInfo.GetValue(window) == false)
            {
                return Visibility.Visible;
            }
        }
        return Visibility.Hidden;
    }

How to test if a dialog is modal or modeless, It blocks the user from opening any other window, just like a modal popup. Using this command, it is not necessary to check if the window is  How do I know what monitor a WPF window is in. Ask Question Asked 10 years, 1 month ago. Determine if an open WPF window is visible on any monitor. 5.

Using UI Automation in Windows, I have come up with something like this:

void Window2_Loaded(object sender, RoutedEventArgs e)
{
    var hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
    var el = AutomationElement.FromHandle(hwnd);

    Object oPattern = null;

    if (el.TryGetCurrentPattern(WindowPattern.Pattern, out oPattern))
    {
        var pattern = oPattern as WindowPattern;

        this.Title = pattern.Current.IsModal.ToString();
    }
}

But this seems not working. There is an IsModal property http://msdn.microsoft.com/en-us/library/system.windows.automation.provider.iwindowprovider.ismodal.aspx there must be a proper way to get AutomationElement for the window and check whether IsModal property of it is true via Automation.

How do I know if a WPF window is opened, How do I check if a modal window with some name say "ModalDialog" exists if I have the I'm not sure that's the right way, but it`s really work in our wpf app Dialog boxes overview. 03/30/2017; 23 minutes to read +9; In this article. Standalone applications typically have a main window that both displays the main data over which the application operates and exposes the functionality to process that data through user interface (UI) mechanisms like menu bars, tool bars, and status bars.

Check If a Modal Window exist or not, How do I determine if a WPF window is modal? What's the easiest way to figure out if a window is opened modally or not? CLARIFICATION: I open a window  One possible way would be to scan the list of open windows in the application and check which one of them has IsActive = true : Not sure if there may be more than one active window if, for example, there's a modal dialog showing, in which case, the owner of the dialog and the dialog itself might be active.

How do I determine if a WPF window is modal?_weixin_30247159 , To determine whether a given Window widget is opened or not, you may check if it is hidden. var wnd = $( "#window" ).data( "kendoWindow" );. How do I determine if a WPF window is modal? 7 answers Can a Window(System.Windows) know whether it is opened by Show() or ShowDialog() ? If DialogResult is set on a window which is opened by Show() method, it throws an InvalidOperationException ( MSDN ).

Teststack White How to Close an MDI Window - white - iOS, Now consider the window state: Normally if we minimize the main window, the child window can’t minimize and similarly maximize. We will manage this scenario on the main window state change event. For minimize, the main window finds out all open child window states using for loop and for maximize, it sets all child window states with foreach.

Comments
  • Exactly what I was looking for. Awesome!
  • Works not immediately after ShowModal is called. Some events still cannot tell if modal ...
  • This doesn't work if a modal dialog is showing this modeless dialog!
  • This is unreliable
  • Tried with a modal dialog created by the main app window, and this approach incorrectly reported "false" in Loaded. Perhaps something changed? The answer by @CMerat gave the correct answer. (Win10, WPF4.5)
  • Not elegant, but a way to detect. Beware if you upgrade .Net framework, Microsoft could try to change the name of private fields such as this one, so your code may break later on.
  • +1 I would think this is preferred to needing to use the IsModal() property too.