wpf 完全自定义的上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13763961/
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 06:31:51  来源:igfitidea点击:

Completely Custom Context Menu

c#wpfcontextmenu

提问by Sarah

I am trying to create a completely custom context menu for a touch application with a radial style ("pie slices" for each item). I have been able to create one from with a base of ItemsControl. However, when I try to switch this to a base of ContextMenu, I cannot get rid of the default pop-up with each pie slice in a list of items.

我正在尝试为具有径向样式(每个项目的“饼图”)的触摸应用程序创建一个完全自定义的上下文菜单。我已经能够用 ItemsControl 的基础创建一个。但是,当我尝试将其切换到 ContextMenu 的基础时,我无法摆脱项目列表中每个饼图切片的默认弹出窗口。

Is there any way to override the default ContextMenu style to get rid of this items list functionality?

有没有办法覆盖默认的 ContextMenu 样式来摆脱这个项目列表功能?

Edit: This is a screen shot of my problem. The code is almost the exact same between the two examples, other than what class is inherited from and how it displays (adding as a child to a Canvas vs. showing the popup)

编辑:这是我的问题的屏幕截图。两个示例之间的代码几乎完全相同,除了从哪个类继承以及它如何显示(作为子项添加到 Canvas 与显示弹出窗口)

Top: The custom context menu inheriting from ItemsControl. Bottom: The same context menu inheriting from ContextMenu

顶部:从 ItemsControl 继承的自定义上下文菜单。 底部:从 ContextMenu 继承的相同上下文菜单

Edit 2: I tried overriding the default style as below:

编辑 2:我尝试覆盖默认样式,如下所示:

<ContextMenu.Style>
  <Style TargetType="ContextMenu">
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ContextMenu">
          <Canvas IsItemsHost="true" Height="100" Width="100" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ContextMenu.Style>

The context menu when overriding the default style.

覆盖默认样式时的上下文菜单。

采纳答案by mathieu

If you just want to change appeareance of a WPF control, just change its controltemplate.

如果您只想更改 WPF 控件的外观,只需更改其控件模板。

Here is the default one for ContextMenu : http://msdn.microsoft.com/en-us/library/ms744758.aspx

这是 ContextMenu 的默认设置:http: //msdn.microsoft.com/en-us/library/ms744758.aspx

And do not forget to update the MenuItem controltemplate too : http://msdn.microsoft.com/en-us/library/ms747082(v=vs.85).aspx

并且不要忘记更新 MenuItem 控件模板:http: //msdn.microsoft.com/en-us/library/ms747082(v=vs.85) .aspx

回答by Charles HETIER

I personaly don't know any way to override or change default contextmenu layout. If ever it appears that there is no proper way, I think you should create it from sratch using Adorner class, otherwise by creating a new transparent tomost window. Keep us informed of the solution you'll choose.

我个人不知道有什么方法可以覆盖或更改默认的上下文菜单布局。如果似乎没有正确的方法,我认为您应该使用 Adorner 类从 sratch 创建它,否则通过创建一个新的透明 tomost 窗口。让我们了解您将选择的解决方案。

Regards

问候