wpf - 子菜单打开而不关闭子菜单时修改 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
偷走焦点?
解决方案
我没有将网格与矩形重叠,而是将我的 2 分成了一半。
- 是菜单栏(屏幕的 10 %)
- 矩形区域(屏幕的 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;
}
}
推荐阅读
- python - 如何删除图像opencv Python中的垂直线
- c++ - 将形状旋转到 1 到 2 弧度之间的任何位置都会重置其旋转
- angularjs - AngularJS 捕获图像网络摄像头
- c++14 - 我不明白“i”代表for循环内的索引
- google-tag-manager - 如何在 Google 跟踪代码管理器中为表单中的按钮构建触发器?
- azure-blob-storage - 无法从 Azure 存储下载大文件
- php - 如何定义while循环导致全局变量并在另一个函数中使用它来处理另一个任务
- javascript - 有没有办法使用 Mocha/Chakram 阻止(失败测试)任何不安全的重定向?
- python - 索引时从列表中删除 '
- csv - 如何在 Nextflow 中访问/操作 splitCSV 运算符的输出?