WPF:如果我将事件处理程序添加到菜单项(在列表框中),则会出现异常

发布于 2024-10-09 19:55:03 字数 1951 浏览 0 评论 0原文

我想要一个 ListBoxItems 的上下文菜单。 所以我创建了这个:

<ListBox Name="listBoxName">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding UserName}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="ContextMenu">
                                <Setter.Value>
                                    <ContextMenu>
                                        <MenuItem Header="View" Name="MenuItemView" />
                                    </ContextMenu>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>

这很好用。我有所有项目的上下文菜单,但如果我想向菜单项添加单击事件处理程序,如下所示:

<MenuItem Header="View" Name="MenuItemView" Click="MenuItemView_Click" />

创建窗口时我收到 XamlParseException。

内部异常: 对象 System.Windows.Controls.MenuItem 无法转换为类型 System.Windows.Controls.Grid

如果我添加事件处理程序,它只会引发异常。事件方法为空。

编辑: 内部异常的堆栈跟踪:

在 Chat_Client.ChatWindow.System.Windows.Markup.IComponentConnector.Connect(Int32 连接Id,对象目标)中 c:\XXX\Chat_Client\ChatWindow.xaml:行 19.

在 MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetConnectionId(对象 根,Int32 连接 ID,对象 实例)

编辑2: 现在我必须获取我通过上下文菜单单击的对象。首先我尝试了这个:

//MenuItem s = sender as MenuItem;
//ContextMenu cm = s.Parent as ContextMenu;
//Popup pu = cm.Parent as Popup;
//object o = pu.Parent;

但是弹出窗口的父级为空。 然后我只需从列表框中获取 selectedItem 即可。这可行,但是没有办法获取单击的上下文菜单的 ListBoxRow 吗?

i wanted a contextmenu for my ListBoxItems.
So i created this:

<ListBox Name="listBoxName">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding UserName}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="ContextMenu">
                                <Setter.Value>
                                    <ContextMenu>
                                        <MenuItem Header="View" Name="MenuItemView" />
                                    </ContextMenu>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ListBox.ItemContainerStyle>
                </ListBox>

This works great. I have the contextmenu for all items, but if i want to add a click-eventhandler to the menuitem, like this:

<MenuItem Header="View" Name="MenuItemView" Click="MenuItemView_Click" />

I get a XamlParseException when the window is created.

InnerException:
The Object System.Windows.Controls.MenuItem cannot be converted to type System.Windows.Controls.Grid

It throws only the exception if i add a event-handler. The event-method is empty.

Edit:
Stacktrace of the InnerException:

at
Chat_Client.ChatWindow.System.Windows.Markup.IComponentConnector.Connect(Int32
connectionId, Object target) in
c:\XXX\Chat_Client\ChatWindow.xaml:Row
19.

at
MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetConnectionId(Object
root, Int32 connectionId, Object
instance)

Edit2:
Now i have to get the object I clicked with the contextmenu. First i tried this:

//MenuItem s = sender as MenuItem;
//ContextMenu cm = s.Parent as ContextMenu;
//Popup pu = cm.Parent as Popup;
//object o = pu.Parent;

But the Popup's parent is null.
Then i simply get the selectedItem from the ListBox. This works, but is there no way to get the ListBoxRow of the clicked Contextmenu?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

旧情勿念 2024-10-16 19:55:03

我无法用 VS2010 和 WPF4 重现您的崩溃。

您的所有项目只需要一个上下文菜单,因此您可以将其提取到窗口资源,例如:

<Window.Resources>
    <ContextMenu x:Key="ListBoxItemContextMenu">
        <MenuItem Header="View" Name="MenuItemView" Click="MenuItemView_Click"/>
    </ContextMenu>
</Window.Resources>

然后更改您的设置器以引用该一个上下文菜单:

<Setter Property="ContextMenu" Value="{StaticResource ListBoxItemContextMenu}"/>

然后事件处理程序就可以工作:

private void MenuItemView_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Clicked!");
}

I cannot reproduce your crash with VS2010 and WPF4.

You only need one context menu for all your items so you can extract it to a window resource, for example:

<Window.Resources>
    <ContextMenu x:Key="ListBoxItemContextMenu">
        <MenuItem Header="View" Name="MenuItemView" Click="MenuItemView_Click"/>
    </ContextMenu>
</Window.Resources>

and then change your setter to refer to that one context menu:

<Setter Property="ContextMenu" Value="{StaticResource ListBoxItemContextMenu}"/>

and the event handler then works:

private void MenuItemView_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Clicked!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文