如何在WPF ContextMenu控件中添加不可选择的上下文菜单项作为标题

时间:2019-01-08 12:32:37

标签: wpf xaml contextmenu

我的UI中有一些符号(代表电气设备)。我实现了一个上下文菜单,该菜单允许用户执行一些操作。

我想在上下文菜单的顶部添加一个不可选择的标题标签;

不应使用鼠标光标选择或突出显示该项目

就像这张图片一样:enter image description here

我该怎么做??

<UserControl.ContextMenu>
    <ContextMenu>

        <MenuItem Header="Start" IsEnabled="{Binding ControlPanelViewModel.IsStartEnabled}" Command="{Binding Path=ControlPanelViewModel.StartEscalatorCommand}"/>
        <MenuItem Header="Stop" IsEnabled="{Binding ControlPanelViewModel.IsStopEnabled}" Command="{Binding Path=ControlPanelViewModel.StopEscalatorCommand}"/>

        <Separator/>

        <MenuItem x:Name="OpenControl" Header="Control panel..." Command="{Binding OpenControlPanelCommand}">
            <MenuItem.Icon>
                <Path Data="M19,4C20.11,4 21,4.9 21,6V18A2,2 0 0,1 19,20H5C3.89,20 3,19.1 3,18V6A2,2 0 0,1 5,4H19M19,18V8H5V18H19Z" Fill="Black" Margin="-5"/>
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</UserControl.ContextMenu>

1 个答案:

答案 0 :(得分:1)

您可以将标题添加到ContextMenu的ControlTemplate。您可以使用解决方案here获取ContextMenu的默认ControlTemplate,然后对其进行修改以添加标题。我在下面完成了此操作:

<UserControl.ContextMenu>
    <ContextMenu>
        <ContextMenu.Template>
            <ControlTemplate TargetType="ContextMenu" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
                <mwt:SystemDropShadowChrome Color="#00FFFFFF" Name="Shdw" SnapsToDevicePixels="True">
                    <Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="ContextMenuBorder">
                        <ScrollViewer Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly=FrameworkElement, ResourceId=MenuScrollViewer}}" Name="ContextMenuScrollViewer" Margin="1,0,1,0" Grid.ColumnSpan="2">
                            <StackPanel>
                                <Label>Your title goes here</Label>
                                <Grid RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas Width="0" Height="0" HorizontalAlignment="Left" VerticalAlignment="Top">
                                        <Rectangle Fill="{x:Null}" Name="OpaqueRect" Width="Auto" Height="Auto" />
                                    </Canvas>
                                    <Rectangle RadiusX="2" RadiusY="2" Fill="#FFF1F1F1" Width="28" Margin="1,2,1,2" HorizontalAlignment="Left" />
                                    <Rectangle Fill="#FFE2E3E3" Width="1" Margin="29,2,0,2" HorizontalAlignment="Left" />
                                    <Rectangle Fill="#FFFFFFFF" Width="1" Margin="30,2,0,2" HorizontalAlignment="Left" />
                                    <ItemsPresenter Name="ItemsPresenter" Margin="{TemplateBinding Control.Padding}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Cycle" />
                                </Grid>
                            </StackPanel>
                        </ScrollViewer>
                    </Border>
                </mwt:SystemDropShadowChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="ContextMenuService.HasDropShadow">
                        <Setter Property="FrameworkElement.Margin" TargetName="Shdw">
                            <Setter.Value>
                                <Thickness>0,0,5,5</Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="mwt:SystemDropShadowChrome.Color" TargetName="Shdw">
                            <Setter.Value>
                                <Color>#71000000</Color>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="ScrollViewer.CanContentScroll" SourceName="ContextMenuScrollViewer">
                        <Setter Property="Canvas.Top" TargetName="OpaqueRect">
                            <Setter.Value>
                                <Binding Path="VerticalOffset" ElementName="ContextMenuScrollViewer" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Canvas.Left" TargetName="OpaqueRect">
                            <Setter.Value>
                                <Binding Path="HorizontalOffset" ElementName="ContextMenuScrollViewer" />
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ContextMenu.Template>

        <MenuItem Header="Start" IsEnabled="{Binding ControlPanelViewModel.IsStartEnabled}" Command="{Binding Path=ControlPanelViewModel.StartEscalatorCommand}"/>
        <MenuItem Header="Stop" IsEnabled="{Binding ControlPanelViewModel.IsStopEnabled}" Command="{Binding Path=ControlPanelViewModel.StopEscalatorCommand}"/>

        <Separator/>

        <MenuItem x:Name="OpenControl" Header="Control panel..." Command="{Binding OpenControlPanelCommand}">
            <MenuItem.Icon>
                <Path Data="M19,4C20.11,4 21,4.9 21,6V18A2,2 0 0,1 19,20H5C3.89,20 3,19.1 3,18V6A2,2 0 0,1 5,4H19M19,18V8H5V18H19Z" Fill="Black" Margin="-5"/>
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</UserControl.ContextMenu>

关键的修改是将Grid包裹在StackPanel中,并在标题Label上方添加一个Grid。由于默认的ControlTemplate使用Aero,因此您需要在项目中引用PresentationFramework.Aero。