Search code examples
wpfwindowfocusmenuitemsubmenu

Modify WPF Window when Submenu opens without closing Submenu


I have a Window with a Menu on it. When the Menu is opened, I would like to change the Window's appearance to look disabled. Simply covering it with a gray Rectangle looks nice. Here is the Window markup:

    <Grid>
        <!--Content-->
        <ContentControl Content="{Binding CurrentViewModel}" />
        <!--Container to hide content-->
        <Rectangle x:Name="Disabler" Fill="#77000000" Visibility="{Binding DisableWindow, Converter={StaticResource BoolToVisibilityConverter}}" />
    </Grid>

I tried to set DisableWindow to true when the Submenu opens and false when it closes. However, setting this value seems to close the Submenu. How can I ensure the Submenu stays open?

    private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
    {
        MainWindowViewModel mainVM = Window.GetWindow(this).DataContext as MainWindowViewModel;
        if (mainVM != null)
        {
            mainVM.DisableWindow = true;
        }
    }

Edit: Since the Rectangle gets set to Visible, the MouseUp event is happening on Disabler. This is why the Submenu closes on me. I tried setting IsHitTestVisible="False" on the Rectangle, but that makes everything under it clickable. Is there a way to keep the Rectangle from stealing focus?

enter image description here


Solution

  • I had to go with setting IsHitTestVisible="False" on the Rectangle, even though that makes everything under it clickable. It's a hack, and I would love a better fix.