wpf contextmenu子菜单放置

时间:2017-04-11 18:32:28

标签: c# wpf

什么决定子菜单在上下文菜单中的位置?

我动态创建一个ContextMenu并设置PlacementRectangle,使其显示在屏幕的中心 - 到目前为止一直很好。

部分项目显示子菜单。 但是,子菜单始终默认显示在左侧。 它应默认显示在所选父项的右侧。

如何更改此内容?

1 个答案:

答案 0 :(得分:0)

这是MenuItem.TopLevelItemTemplateKey

的默认值
<Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Bottom" >

现在这是MenuItem.SubmenuHeaderTemplateKey

的默认值
 <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" HorizontalOffset="-2" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" Placement="Right" VerticalOffset="-3">

编辑1: 查找App.xaml文件中定义的弹出窗口的一些自定义样式,默认情况下可以设置屏幕中心所有弹出窗口的位置

编辑2: 如果您使用带触摸屏的窗口,菜单会出现在与右手或左手定义的方向相反的位置,这样在使用时菜单不会被手遮挡 click here to see how to fix it