在 WPF 中自定义上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15675382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:21:19  来源:igfitidea点击:

Customizing Context Menu in WPF

wpfxamlcontextmenu

提问by Alan Ho

I have a project here where I require customizing the context menu in my WPF application in which a button would be placed at the bottom of all the menuitems.

我在这里有一个项目,我需要在我的 WPF 应用程序中自定义上下文菜单,其中一个按钮将放置在所有菜单项的底部。

However, if I were to add the button through the XAML, it would appear as another item in the collection in the context menu and the mouse-over highlight would act on it.

但是,如果我要通过 XAML 添加按钮,它将在上下文菜单中显示为集合中的另一个项目,并且鼠标悬停突出显示将作用于它。

I would like to have the context menu tuned to a grid-like style whereby I could customize the style underneath it.

我希望将上下文菜单调整为类似网格的样式,以便我可以自定义其下方的样式。

Any idea how I can achieve this (preferably in the XAML)?

知道如何实现这一点(最好在 XAML 中)吗?

回答by Ben Wilde

It's actually pretty straightforward in the XAML. Just define it under the element for which you want to have the context menu.

它实际上在 XAML 中非常简单。只需在您想要拥有上下文菜单的元素下定义它。

        <Border>
            <Border.ContextMenu>
                <ContextMenu>
                    <ContextMenu.Template>
                        <ControlTemplate>
                            <Grid>
                                <!--Put anything you want in here.-->
                            </Grid>
                        </ControlTemplate>
                    </ContextMenu.Template>
                </ContextMenu>
            </Border.ContextMenu>
        </Border>

回答by Chrisjan Lodewyks

For your menu item style with the button in the item you can use the following code:

对于带有项目中按钮的菜单项样式,您可以使用以下代码:

Note - Adding items to the Headerwill keep it in the same MenuItem, but if added to the MenuItemonly it will be regarded as a new MenuItem.

注 - 向 中添加项目Header将使其保持不变MenuItem,但如果添加到MenuItem唯一中,它将被视为新的MenuItem

<ContextMenu>
    <ContextMenu.Items>
       <MenuItem>
          <MenuItem.Header>
             <StackPanel>
                <TextBlock Text="Item 1"/>
                <Button Content="Button 1" Margin="5"/>
             </StackPanel>
          </MenuItem.Header>
        </MenuItem>
        <MenuItem>
          <MenuItem.Header>
             <StackPanel>
                <TextBlock Text="Item 2"/>
                <Button Content="Button 2" Margin="5"/>
              </StackPanel>
           </MenuItem.Header>
          </MenuItem>
     </ContextMenu.Items>
 </ContextMenu>

This will be the resulting ContextMenu:

这将是结果ContextMenu

enter image description here

在此处输入图片说明

From there you can style the MenuItemor Buttonetc.

从那里你可以设计MenuItemButton等。

Hope it helps!

希望能帮助到你!

回答by Drew Noakes

You can start with the example style/template (from here) for ContextMenuand adjust it to your needs.

您可以从示例样式/模板(从这里开始)开始ContextMenu并根据您的需要进行调整。

<Style TargetType="{x:Type ContextMenu}">
  <Setter Property="SnapsToDevicePixels" Value="True" />
  <Setter Property="OverridesDefaultStyle" Value="True" />
  <Setter Property="Grid.IsSharedSizeScope" Value="true" />
  <Setter Property="HasDropShadow" Value="True" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ContextMenu}">
        <Border x:Name="Border"
                Background="{StaticResource MenuPopupBrush}"
                BorderThickness="1">
          <Border.BorderBrush>
            <SolidColorBrush Color="{StaticResource BorderMediumColor}" />
          </Border.BorderBrush>
          <StackPanel IsItemsHost="True"
                      KeyboardNavigation.DirectionalNavigation="Cycle" />
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="HasDropShadow" Value="true">
            <Setter TargetName="Border" Property="Padding" Value="0,3,0,3" />
            <Setter TargetName="Border" Property="CornerRadius" Value="4" />
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>