Alex Rivera | Logout

How do I determine if a WPF window is modal?

Asked 2008-12-15T16:31:35.023
35

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?

Edit
Report

1 Answer

0

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.

answered 2008-12-15T17:18:43.350

Your Answer