
{"id":434,"date":"2010-04-14T14:04:23","date_gmt":"2010-04-14T18:04:23","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=434"},"modified":"2010-04-14T14:04:23","modified_gmt":"2010-04-14T18:04:23","slug":"wpf-context-menu-on-list-item","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=434","title":{"rendered":"WPF: Context menu on list item"},"content":{"rendered":"<p>I am using WPF and MVVM. I have a Window and a view model attached to it via <code>Datacontext<\/code>. The window has a listbox, and its items have context menu. I am using <code>DelegateCommand<\/code> in my view model, and I want to bind a menu items in the context menu to this command.<\/p>\n<p>First trouble is, by default the menu item is bound to the <code>DataContext<\/code> of the list box item, which may or may not have knowledge of the whole view model.<\/p>\n<p>Second trouble is, context menu is not part of the visual tree, so referring to the main window via <code>FindAncestor<\/code> or <code>ElementName=...<\/code> will not work.<\/p>\n<p>You can get to the object immediately containing the context menu via the <code>PlacementTarget<\/code> property, but then you will need an ancestor of this object (&#8220;window&#8221;), and there is no built-in way to find an ancestor of a property.<\/p>\n<p>An (almost) working solution that I found on the Internet after hours of search was to store the data context in some property of the <code>PlacementTarget<\/code> &#8211; they used <code>Tag<\/code> for this purpose.<\/p>\n<p>The &#8220;almost&#8221; part lies in the fact that if your command has <code>CanExecute()<\/code> method, the command parameter passed to it is sometimes <code>null<\/code> (see <a href=\"http:\/\/stackoverflow.com\/questions\/335849\/wpf-commandparameter-is-null-first-time-canexecute-is-called\">here<\/a>).<\/p>\n<pre><code>&lt;Window Name=<font color=\"red\"><b>\"MainWindow\"<\/b><\/font> ...&gt;\n    &lt;Grid&gt;\n        &lt;ListBox ItemsSource=\"{Binding Items}\"&gt;\n            &lt;ListBox.ItemTemplate&gt;\n                &lt;DataTemplate&gt;\n                    &lt;StackPanel Orientation=\"Horizontal\"\n                               <font color=\"red\"><b>Tag=\"{Binding DataContext, ElementName=MainWindow}\"<\/b><\/font>&gt;\n\t\t\t...\n                        &lt;StackPanel.ContextMenu&gt;\n                             &lt;MenuItem Header=\"do it\"\n    Command=<font color=\"red\"><b>\"{Binding PlacementTarget.Tag.MyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}\"<\/b><\/font>\n    CommandParameter=<font color=\"red\"><b>\"{Binding}\"<\/b><\/font>&gt;\n                        &lt;\/StackPanel.ContextMenu&gt;\n                    &lt;\/StackPanel&gt;\n                &lt;\/DataTemplate&gt;\n            &lt;\/ListBox.ItemTemplate&gt;\n        &lt;\/ListBox&gt;\n    &lt;\/Grid&gt;\n&lt;\/Window&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am using WPF and MVVM. I have a Window and a view model attached to it via Datacontext. The window has a listbox, and its items have context menu. <a href=\"https:\/\/ikriv.com\/blog\/?p=434\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[12],"tags":[],"class_list":["entry","author-ikriv","post-434","post","type-post","status-publish","format-standard","category-wpf"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=434"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/434\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}