wpf 如何隐藏功能区控件的上下文菜单

lb3vh1jj  于 2023-03-04  发布在  其他
关注(0)|答案(2)|浏览(213)

我需要隐藏功能区控件的上下文菜单

我试过了

<Ribbon.ContextMenu><ContextMenu Visibility="Hidden"></ContextMenu></Ribbon.ContextMenu>

这在一定程度上完成了这项工作。即它禁用上下文菜单时,右键单击空白空间。但当我右键单击功能区按钮,仍然出现上下文菜单。我怎么能禁用这一点?

kninwzqo

kninwzqo1#

Ribbon就是一个Ribbon,你不应该禁用它的功能,如果你的“Ribbon”真的看起来像这样,考虑使用工具栏。
也就是说,您可以通过在所有层次结构级别上的所有控件上将上下文菜单设置为null来禁用它们:

<Ribbon ContextMenu="{x:Null}">
    <RibbonTab Header="abc" ContextMenu="{x:Null}">
        <RibbonGroup Header="abc" ContextMenu="{x:Null}">
            <RibbonButton Content="abc" ContextMenu="{x:Null}"/>
        </RibbonGroup>
    </RibbonTab>
</Ribbon>
l7mqbcuq

l7mqbcuq2#

您可以为所有功能区项目添加样式到app.xaml Under<Application.Resources>。请参见下面的示例。

<Application.Resources>
    <ContextMenu x:Key="HiddenContextMenu" Visibility="Hidden"/>
    <Style TargetType="Ribbon">
        <Setter Property="Ribbon.ContextMenu" Value="{StaticResource HiddenContextMenu}"/>
    </Style>
    <Style TargetType="RibbonTab">
        <Setter Property="RibbonTab.ContextMenu" Value="{StaticResource HiddenContextMenu}"/>
    </Style>
    <Style TargetType="RibbonGroup">
        <Setter Property="RibbonGroup.ContextMenu" Value="{StaticResource HiddenContextMenu}"/>
    </Style>
    <Style TargetType="RibbonButton">
        <Setter Property="RibbonButton.ContextMenu" Value="{StaticResource HiddenContextMenu}"/>
    </Style>
    <Style TargetType="RibbonRadioButton">
        <Setter Property="RibbonRadioButton.ContextMenu" Value="{StaticResource HiddenContextMenu}"/>
    </Style>
    <Style TargetType="RibbonTextBox">
        <Setter Property="RibbonTextBox.ContextMenu" Value="{StaticResource HiddenContextMenu}"/>
    </Style>
    <Style TargetType="RibbonComboBox">
        <Setter Property="RibbonComboBox.ContextMenu" Value="{StaticResource HiddenContextMenu}"/>
    </Style>
</Application.Resources>

相关问题