开发者

WPF: Exception if i add a eventhandler to a MenuItem (in a ListBox)

开发者 https://www.devze.com 2023-02-01 22:50 出处:网络
i wanted a contextmenu for my ListBoxItems. So i created this: <ListBox Name=\"listBoxName\"> <ListBox.ItemTemplate>

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?


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!");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消