首页 > 解决方案 > 将上下文菜单的可见性设置为隐藏/折叠时出现空白上下文菜单

问题描述

我想在上下文菜单中不显示上下文菜单或单个项目或两个项目。我正在使用 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 菜单项都按预期显示。

标签: wpfcontextmenudatatrigger

解决方案


ContextMenu您可以在设置器中将ContextMenu属性设置为,而不是显示一个空null的:

<Setter Property="ContextMenu" Value="{x:Null}"/>

如果该ContextMenuEnabled属性是在您的视图模型中定义的,您应该使用 aRelativeSource来绑定它:

Binding="{Binding DataContext.ContextMenuEnabled, 
   RelativeSource={RelativeSource AncestorType=UserControl}}"

推荐阅读