Wpf- Unable to cast MenuItem to Listbox?
I got the same exception as you did. After looking closer at the code, this feels like a situation where you would get
"The event 'Click' cannot be specified on a Target tag in a Style. Use an EventSetter instead."
I'm not sure why that doesn't apply here.
Anyway, using an EventSetter works
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Name="AddProjectElementMenuItem" Header="Add">
<MenuItem.Style>
<Style TargetType="MenuItem">
<EventSetter Event="Click" Handler="AddProjectElementMenuItem_Click"/>
</Style>
</MenuItem.Style>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
I had to face this weird situation myself. There is an easy way to get over it, you have to clean and rebuild the project and the exception will go away.
Hope this helps.