【问题标题】:Modify WPF Window when Submenu opens without closing Submenu子菜单打开而不关闭子菜单时修改 WPF 窗口
【发布时间】:2019-05-06 05:33:08
【问题描述】:

我有一个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 设置为 true,在它关闭时设置为 false。但是,设置此值似乎会关闭子菜单。如何确保子菜单保持打开状态?

    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 上发生。这就是子菜单对我关闭的原因。我尝试在Rectangle 上设置 IsHitTestVisible="False",但这使得它下面的所有内容都可以点击。有没有办法防止Rectangle 窃取焦点?

【问题讨论】:

    标签: wpf window focus menuitem submenu


    【解决方案1】:

    我不得不在 Rectangle 上设置 IsHitTestVisible="False",即使这使得它下面的所有内容都可以点击。这是一个 hack,我希望得到更好的修复。

    【讨论】:

      【解决方案2】:

      我没有将网格与矩形重叠,而是将我的 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;
          }
      }
      

      查看下方的菜单点击图片。

      【讨论】:

      • 不幸的是,我也必须禁用菜单,但你说得很好。看起来我的矩形正在窃取 MouseUp 事件。
      • @RachelMartin,现在结帐,我添加了模糊效果,它甚至可以覆盖菜单栏。
      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2011-11-14
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多