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

I cannot reproduce your crash with VS2010 and WPF4.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

I wanted a contextmenu for my ListBoxItems. So I created this: This works great. I have the contextmenu for all items, but if I want to add a click-eventhandler to the menuitem, like this: 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? Wpf exception listbox contextmenu link|improve this question edited Dec 30 '10 at 13:53 asked Dec 30 '10 at 2:50user437899219213 86% accept rate.

– SLaks Dec 30 '10 at 2:54 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) – user437899 Dec 30 '10 at 3:05.

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: and then change your setter to refer to that one context menu: and the event handler then works: private void MenuItemView_Click(object sender, RoutedEventArgs e) { Debug. WriteLine("Clicked!

"); }.

Thank you Rick. I created a new demo-project to reproduce the error, but ne exception this time... However, the event got not triggered. (I had the Conextmenu defined in the listbox) After I extracted the Contextmenu to Windows.

Ressources it worked! But why did it not worked when the contextmenu is defined in the listbox? – user437899 Dec 30 '10 at 13:49.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions