Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if a WPF window is modal?

Tags:

.net

wpf

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?

like image 701
Micah Avatar asked Dec 15 '08 16:12

Micah


People also ask

What is the difference between WPF window and WPF page?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

Is a modal window a child window?

A modal window is a child window that requires the user to interact with it before they can return to operating the parent application, thus preventing any work on the application main window.

How do I know if a WPF window is open?

In WPF there is a collection of the open Windows in the Application class, you could make a helper method to check if the window is open. Here is an example that will check if any Window of a certain Type or if a Window with a certain name is open, or both. Show activity on this post. Show activity on this post.


1 Answers

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.

like image 190
CMerat Avatar answered Sep 20 '22 04:09

CMerat