How to disable keyboard and mouse events in a part of the visual tree without using IsEnabled?

I have a requirement for creating pseudo-modal dialogs in WPF. That is, for some specific (technical) reasons, software is not allowed to create modal dialogs. Instead, the user should interact with the "built-in" modal dialogs when necessary.

I found a solution that works well with MVVM and takes care of the dispatcher and the synchronous nature of modal dialogs. However, I ran into the problem of disabling user input in the background GUI. Unfortunately, setting all controls to IsEnabled = false is unacceptable, since it changes the visual state of the background controls (grayscale → poor readability).

Is there a direct way to turn off user input (including focus and keyboard) in the background without changing the visual state?

Thank you for your help!

+3
source share
4 answers

github, FrameworkElement, .

:

<c:ModalContentPresenter IsModal="{Binding DialogIsVisible}">
    <TabControl Margin="5">
            <Button Margin="55"
                    Padding="10"
                    Command="{Binding ShowModalContentCommand}">
                This is the primary Content
            </Button>
        </TabItem>
    </TabControl>

    <c:ModalContentPresenter.ModalContent>
        <Button Margin="75"
                Padding="50"
                Command="{Binding HideModalContentCommand}">
            This is the modal content
        </Button>
    </c:ModalContentPresenter.ModalContent>

</c:ModalContentPresenter>

:

  • .
  • , .
  • , .
  • , , .
  • MVVM IsModal.
+2

.IsHitTestVisible, mouseclicks /. , .IsEnabled, ( ).

+4

( MVVM). overlay UserControl . ( IsEnabled = false - , , toggling IsEnabled .)

() . " " - - - :

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

+3

, "" "", , , :

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

.

, Trigger IsEnabled , . , - Visual Studio .:)

:

I'm worried this answer is getting a bit complicated, but you can set the IsTabStop and Focusable properties to false on your controls to get this behavior.

0
source

Source: https://habr.com/ru/post/1712369/


All Articles