ContextMenu ItemsSource 绑定问题
wpf 用户控件的 ContextMenu 存在问题。我写了一个用户控件,这个用户控件有一个windowsformhost,我想用wpf上下文菜单处理windows控件的右键上下文菜单。
因此,我向 wpf 用户控件发送消息以调用 contextMenu.IsOpen = true,然后填充 contextMenu.ItemsSourceProperty 绑定。但是当我调用 contextmenu.IsOpen = true, contextMenu.Items.Count == 0 时,我该如何解决这个问题?
这是我的代码:
<UserControl x:Class="ControlEase.Inspec.Drawing.CanvasEditorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ControlEase.Inspec.Drawing"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding Command}"/>
</Style>
<ContextMenu x:Key="graphicsMenu" ItemsSource="{Binding Commands}"/>
</UserControl.Resources>
<UserControl.ContextMenu>
<ContextMenu ContextMenu="{StaticResource graphicsMenu}"/>
</UserControl.ContextMenu>
<Grid>
<local:InitializedUserControl local:LoaderHelper.InitializedCommand="{Binding OpenCommand}" >
<WindowsFormsHost x:Name="windowsHost" local:CanvasContainerExtensions.Canvas="{Binding Model}"/>
<EventCommander.Mappings>
<CommandEvent Event="GotFocus" Command="{Binding ActiveCommand}"/>
<CommandEvent Event="LostFocus" Command="{Binding DeActiveCommand}"/>
</EventCommander.Mappings>
</local:InitializedUserControl>
</Grid>
当我在 xaml.cs 中收到消息时,我调用 ContextMenu.IsOpen = true。 ContextMenu != null,但 ContextMenu.Itmes.Count == 0,并且输出板中没有绑定错误。请帮忙。
There is a problem with ContextMenu with wpf usercontrol. I wrote a usercontrol, and this usercontrol have a windowsformhost, and i want to handle the right button contextmenu of the windows control with wpf context menu.
So i send a message to wpf user control to call contextMenu.IsOpen = true, and i fill the contextMenu.ItemsSourceProperty a binding. But when i call the contextmenu.IsOpen = true, The contextMenu.Items.Count == 0, how can i solve this problem?
Here is my code:
<UserControl x:Class="ControlEase.Inspec.Drawing.CanvasEditorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ControlEase.Inspec.Drawing"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Name}"/>
<Setter Property="Command" Value="{Binding Command}"/>
</Style>
<ContextMenu x:Key="graphicsMenu" ItemsSource="{Binding Commands}"/>
</UserControl.Resources>
<UserControl.ContextMenu>
<ContextMenu ContextMenu="{StaticResource graphicsMenu}"/>
</UserControl.ContextMenu>
<Grid>
<local:InitializedUserControl local:LoaderHelper.InitializedCommand="{Binding OpenCommand}" >
<WindowsFormsHost x:Name="windowsHost" local:CanvasContainerExtensions.Canvas="{Binding Model}"/>
<EventCommander.Mappings>
<CommandEvent Event="GotFocus" Command="{Binding ActiveCommand}"/>
<CommandEvent Event="LostFocus" Command="{Binding DeActiveCommand}"/>
</EventCommander.Mappings>
</local:InitializedUserControl>
</Grid>
And when i get the message in the xaml.cs, i called ContextMenu.IsOpen = true. The ContextMenu != null, but ContextMenu.Itmes.Count == 0, and there is no binding error in the output pad. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,您要设置
ContextMenu
的ContextMenu
属性:因此,您将得到一个空的外部
ContextMenu
,其中包含 < code>ContextMenu 自己的,包含您需要的项目。外部上下文菜单永远不会显示,因为它没有任何内容,但如果它显示并且渲染得足够大,您可以右键单击它以查看内部上下文菜单,这是您的菜单项隐藏的地方。ContextMenu
资源是多余的。你可以这样:Here, you're setting the
ContextMenu
property of theContextMenu
:As a result, you'll have an empty outer
ContextMenu
, with aContextMenu
of its own that contains the items you need. The outer context menu won't ever display because it has nothing in it, but if it did and it rendered large enough, you could right-click it to see the inner context menu, which is where your menu items would be hiding away.The
ContextMenu
resource is redundant. You could just have this: