WPF: Exception if i add a eventhandler to a MenuItem (in a ListBox)
- by user437899
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.