tags:

views:

537

answers:

2

I've the requirement to build pseudo-modal dialogs in WPF. That is, for some specific (technical) reasons the software is not allowed to spawn modal dialogs. Instead, the user should interact with "embedded" modal dialogs when necessary.

I found a solution that works pretty well with MVVM and takes care of the Dispatcher and the synchronous character of modal dialogs. However, I am facing a problem with disabling the user input in the background GUI. Setting all controls to IsEnabled = false is unfortunately not acceptable since it changes the visual state of the background controls (shades of grey ->bad readability).

Is there a straight forward way to disable the user input (including focus and keyboard) in the background without changing the visual state?

Thanks for your help!

A: 

A partial solution could be to make your "dialog" control cover your entire application's Window, but have most of it be transparent, with opaque dialog content in the middle, like:

+----------------+
|                |
|  Transparent   |
|                |
|  +----------+  |
|  | dialog   |  |
|  | content  |  |
|  +----------+  |
|                |
+----------------+

But, that is a bit of a hack.

To directly address your question, you can use a Trigger on the IsEnabled property on your controls to keep the colors from changing. Perhaps someone with Visual Studio on their box can provide a code sample. :)

Response to your comment:

I worry that this answer is getting a bit tricky, but you can set the IsTabStop and Focusable properties to false on your controls to get that behavior.

GreenReign
Thanks for you response! Unfortunately, putting a transparent layer into the app is only half the solution (that's where I am now). My problem is to disable the keyboard focus in the layer below the transparent layer. It is possible to tab to controls that are below the transparent surface :-)
olli
You can set the IsTabStop and Focusable properties to false on your controls to get that behavior ... though, I worry this solution is not very elegant.
GreenReign
A: 

I've been struggling with the same issue (also MVVM). I'm also using a UserControl overlay instead of a modal popup. (In my case, I dislike IsEnabled=false not because of the disabled style, but because toggling IsEnabled makes it hard to get keyboard focus back.)

I'm using the overlay solution (above) for blocking mouse action. And for the "other half of the solution" - -disabling keyboard input -- I'm handing this in the main window:


Window
+----------------+  private void Window_PreviewKeyDown(object sender,
|                |                                     KeyEventArgs e){
|  Transparent   |      if (this.myDialog.Visibility == Visibility.Visible){
|                |          e.Handled = true;
|  +----------+  |      }
|  | myDialog |  |  }
|  | content  |  |
|  +----------+  |
|                |
+----------------+

Jeffrey Knight