首页 > 解决方案 > 子菜单打开而不关闭子菜单时修改 WPF 窗口

问题描述

我有Window一个Menu就可以了。打开时Menu,我想将Window的外观更改为禁用。简单地用灰色覆盖它Rectangle看起来不错。这是Window标记:

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

我试图DisableWindow在子菜单打开时设置为真,在它关闭时设置为假。但是,设置此值似乎会关闭子菜单。如何确保子菜单保持打开状态?

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

编辑:由于Rectangle设置为可见,MouseUp 事件正在 Disabler 上发生。这就是为什么子菜单对我关闭的原因。我尝试在 上设置 IsHitTestVisible="False" Rectangle,但这使得它下面的所有内容都可以点击。有没有办法防止Rectangle偷走焦点?

在此处输入图像描述

标签: wpfwindowfocusmenuitemsubmenu

解决方案


我没有将网格与矩形重叠,而是将我的 2 分成了一半。

  1. 是菜单栏(屏幕的 10 %)
  2. 矩形区域(屏幕的 90%)

在此处输入图像描述

屏幕Xaml

 <Window x:Class="WpfApp4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp4"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d" x:Name="Window1"
    Title="MainWindow" Height="450" Width="800">

<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="35"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Border>
        <Border.Effect>
            <BlurEffect Radius="{Binding ElementName=Window1,Path=DataContext.Radius}" KernelType="Gaussian"/>
        </Border.Effect>
        <Menu Grid.Row="0" x:Name="Menubar" HorizontalAlignment="Left" Height="24" Margin="10,10,0,0" VerticalAlignment="Top" Width="772"   >
            <MenuItem Header="Home" SubmenuOpened="MenuItem_SubmenuOpened" SubmenuClosed="MenuItem_SubmenuClosed" >
                <MenuItem Header="Office" >
                    <MenuItem Header="Ground Floor"/>
                </MenuItem>

                <MenuItem Header="Exit" />
            </MenuItem>
        </Menu>
    </Border>
    <Rectangle Grid.Row="1"  x:Name="Disabler" Fill="{Binding ElementName=Window1, Path=DataContext.BackGroundColor}"   />

</Grid>

正如您在 Xaml 中看到的,我使用了 2 个事件 SubmenuOpened 和 SubmenuClosed。这 2 个方法负责翻转矩形填充画笔颜色。

在 ViewModel/CodeBehind 中,我创建了 1 个名为 BackGroundColor 的属性,当未单击菜单时,该属性将显示为白色,如果单击菜单,则显示为灰色。BackGroundColor 将与 Rectangle 的 Fill 属性绑定。

代码背后

     public partial class MainWindow : Window,INotifyPropertyChanged
{
    private Brush _backGroundcolor;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public int _radius { get; set; }

    public int Radius
    {
        get
        {
            return _radius;
        }
        set
        {
            _radius = value;
            NotifyPropertyChanged(nameof(Radius));
        }
    }

    public Brush BackGroundColor
    {
        get
        {
            return _backGroundcolor;
        }
        set
        {
            _backGroundcolor = value;
            NotifyPropertyChanged(nameof(BackGroundColor));

        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
    {
        BackGroundColor = new SolidColorBrush(Colors.Gray);
        Radius = 5;
    }

    private void MenuItem_SubmenuClosed(object sender, RoutedEventArgs e)
    {
        BackGroundColor = new SolidColorBrush(Colors.White);
        Radius = 0;
    }
}

查看下面的菜单点击图片。 在此处输入图像描述


推荐阅读