wpf - 将上下文菜单的可见性设置为隐藏/折叠时出现空白上下文菜单
问题描述
我想在上下文菜单中不显示上下文菜单或单个项目或两个项目。我正在使用 MVVM 模式。
I have tried linking the context menu visibility to a bool property, this appears to work, but an empty context menu is rendered behind the current control, and becomes visible once the current control is closed. I've tried adding a data trigger bound to the same property - but this does not get fired. examining the visual tree shows that the default property is set for the context menu.
MenuItem Visibility works fine, so I can get 1 or 2 items displaying. But when No context menu is needed, a blank menu appears behind
Note: same results with Visibility Hidden or Collapsed
The context menu is bound to a DataGrid as a static resource:
RowStyle="{StaticResource PagedGridRowStyle}"
<ContextMenu x:Key="BlankMenu" Visibility="Hidden">
</ContextMenu>
<ContextMenu x:Key="PagedGridMenu"
Visibility="{Binding Path=DataContext.ContextMenuEnabled, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource VisConverter}}"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Header="{Binding Path=DataContext.MenuActionName1, RelativeSource={RelativeSource AncestorType=UserControl}}"
Visibility="{Binding Path=DataContext.ContextMenuEnabled, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource VisConverter}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext}"
Command="{Binding Path=DataContext.MenuActionCommand1, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
<MenuItem Header="{Binding Path=DataContext.MenuActionName2, RelativeSource={RelativeSource AncestorType=UserControl}}"
Visibility="{Binding Path=DataContext.ContextMenu2Enabled, RelativeSource={RelativeSource AncestorType=UserControl}, Converter={StaticResource VisConverter}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext}"
Command="{Binding Path=DataContext.MenuActionCommand2, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</ContextMenu>
<Style x:Key="PagedGridRowStyle"
TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu"
Value="{StaticResource PagedGridMenu}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ContextMenuEnabled}" Value="false">
<Setter Property="ContextMenu" Value="{StaticResource BlankMenu}"/>
</DataTrigger>
</Style.Triggers>
</Style>
Note VisConverter simply toggles Visibility.Visible and Visibility.Collapsed on the boolean value
Style.Triggers - 上面:这是一个替换完整上下文菜单的尝试 - 试图避免在控件后面打开空白上下文菜单。- 但它不会被解雇。
private bool _contextMenu2Enabled;
public bool ContextMenu2Enabled
{
get => _contextMenu2Enabled;
set
{
_contextMenu2Enabled = value;
OnPropertyChanged();
}
}
private bool _contextMenuEnabled;
public bool ContextMenuEnabled
{
get => _contextMenuEnabled;
set
{
_contextMenuEnabled = value;
OnPropertyChanged();
}
}
绑定错误:
当 ContextMenuEnabled = false 时:(并且 ContextMenu2Enabled = false)
System.Windows.Data Error: 40 : BindingExpression path error: 'ContextMenuEnabled' property not found on 'object' ''DataRowView' (HashCode=33440573)'. BindingExpression:Path=ContextMenuEnabled; DataItem='DataRowView' (HashCode=33440573); target element is 'DataGridRow' (Name=''); target property is 'NoTarget' (type 'Object')
上下文菜单不出现,但在数据网格后面呈现一个空的上下文菜单,并在数据网格关闭后变为可见
当 ContextMenuEnabled = true(并且 ContextMenu2Enabled = false)时,同样的 BindingExpression 错误 - 但上下文菜单按预期显示单个项目。
当 ContextMenuEnabled = true 和 ContextMenu2Enabled = true 时出现同样的 BindingExpression 错误,但两个 Context 菜单项都按预期显示。
解决方案
ContextMenu
您可以在设置器中将ContextMenu
属性设置为,而不是显示一个空null
的:
<Setter Property="ContextMenu" Value="{x:Null}"/>
如果该ContextMenuEnabled
属性是在您的视图模型中定义的,您应该使用 aRelativeSource
来绑定它:
Binding="{Binding DataContext.ContextMenuEnabled,
RelativeSource={RelativeSource AncestorType=UserControl}}"
推荐阅读
- amazon-ses - 使用 ses 创建 ETS 模型但是此错误不断弹出
- javascript - 如何在 JavaScript 中获取 gif 图像的持续时间
- python - executor.shutdown 中的“方法返回”是什么意思?
- spring-boot - FxRobot (TestFx) 查询不返回节点
- javascript - 为什么它忽略了数字 7?
- python - 在树中查找值的路径
- express - Node express 服务器卡住了
- python - 更改子模型 Django 中的选择列表
- python - 我正在制作一个 on_member_join 并删除事件,但如果机器人在任何其他服务器中并且有人加入该服务器,它会将其发送到他们加入的我的服务器
- python - 如何找到 7 种资产的平均回撤?