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

Posted by user437899 on Stack Overflow See other posts from Stack Overflow or by user437899
Published on 2010-12-30T02:50:48Z Indexed on 2010/12/30 2:54 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

Hi,

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.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about exception