ContextMenu ItemsSource 绑定问题

发布于 2024-12-06 14:11:09 字数 1973 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤浅笑 2024-12-13 14:11:09

在这里,您要设置 ContextMenuContextMenu 属性:

<ContextMenu ContextMenu="{StaticResource graphicsMenu}"/>

因此,您将得到一个空的外部 ContextMenu,其中包含 < code>ContextMenu 自己的,包含您需要的项目。外部上下文菜单永远不会显示,因为它没有任何内容,但如果它显示并且渲染得足够大,您可以右键单击它以查看内部上下文菜单,这是您的菜单项隐藏的地方。

ContextMenu 资源是多余的。你可以这样:

<UserControl.ContextMenu>
    <ContextMenu ItemsSource="{Binding Commands}"/>
</UserControl.ContextMenu>

Here, you're setting the ContextMenu property of the ContextMenu:

<ContextMenu ContextMenu="{StaticResource graphicsMenu}"/>

As a result, you'll have an empty outer ContextMenu, with a ContextMenu 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:

<UserControl.ContextMenu>
    <ContextMenu ItemsSource="{Binding Commands}"/>
</UserControl.ContextMenu>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文