Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Exception if i add a eventhandler to a MenuItem (in a ListBox)
    primarykey
    data
    text
    <p>i wanted a contextmenu for my ListBoxItems. So i created this:</p> <pre><code>&lt;ListBox Name="listBoxName"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding UserName}" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;ListBox.ItemContainerStyle&gt; &lt;Style TargetType="ListBoxItem"&gt; &lt;Setter Property="ContextMenu"&gt; &lt;Setter.Value&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="View" Name="MenuItemView" /&gt; &lt;/ContextMenu&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ListBox.ItemContainerStyle&gt; &lt;/ListBox&gt; </code></pre> <p>This works great. I have the contextmenu for all items, but if i want to add a click-eventhandler to the menuitem, like this:</p> <pre><code>&lt;MenuItem Header="View" Name="MenuItemView" Click="MenuItemView_Click" /&gt; </code></pre> <p>I get a XamlParseException when the window is created.</p> <p>InnerException: The Object System.Windows.Controls.MenuItem cannot be converted to type System.Windows.Controls.Grid</p> <p>It throws only the exception if i add a event-handler. The event-method is empty.</p> <p>Edit: Stacktrace of the InnerException:</p> <blockquote> <p>at Chat_Client.ChatWindow.System.Windows.Markup.IComponentConnector.Connect(Int32 connectionId, Object target) in c:\XXX\Chat_Client\ChatWindow.xaml:Row 19.</p> <p>at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetConnectionId(Object root, Int32 connectionId, Object instance)</p> </blockquote> <p>Edit2: Now i have to get the object I clicked with the contextmenu. First i tried this:</p> <pre><code>//MenuItem s = sender as MenuItem; //ContextMenu cm = s.Parent as ContextMenu; //Popup pu = cm.Parent as Popup; //object o = pu.Parent; </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload