{"id":25,"date":"2010-05-24T14:21:55","date_gmt":"2010-05-24T14:21:55","guid":{"rendered":"http:\/\/www.benbarefield.com\/blog\/?p=25"},"modified":"2010-05-24T14:21:55","modified_gmt":"2010-05-24T14:21:55","slug":"wpf-lessons-learned-use-contextmenu-items-over-contextmenu-itemssource","status":"publish","type":"post","link":"https:\/\/www.benbarefield.com\/blog\/2010\/05\/24\/wpf-lessons-learned-use-contextmenu-items-over-contextmenu-itemssource\/","title":{"rendered":"WPF Lessons Learned &#8211; Use ContextMenu.Items over ContextMenu.ItemsSource"},"content":{"rendered":"<p>I&#8217;ve been working on a WPF project for a client and have learned a lot in the process. I&#8217;d like to touch on some of these things. This first post will be about creating a ContextMenu.<\/p>\n<p>I&#8217;ve had to create more than one custom ContextMenu for this project. In my first attempt, I created a List&lt;MenuItem&gt; and added all of the MenuItems I wanted in my ContextMenu to this List. Then I created a new ContextMenu and set the ItemsSource to the List. In the MenuItems&#8217; Click event I would need access to the UI element that opened the menu, but when I went to try to get at this element I found that the MenuItem.Parent was null. <\/p>\n<p>When I changed my method to creating a new ContextMenu and then adding all of my MenuItems to it by using ContextMenu.Items.Add(), all of them have the ContextMenu as a Parent. ContextMenu has a PlacementTarget property that is the Object that caused the ContextMenu to open, so my problems were solved.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a WPF project for a client and have learned a lot in the process. I&#8217;d like to touch on some of these things. This first post will be about creating a ContextMenu. I&#8217;ve had to create &hellip; <a href=\"https:\/\/www.benbarefield.com\/blog\/2010\/05\/24\/wpf-lessons-learned-use-contextmenu-items-over-contextmenu-itemssource\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7,21],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-wpf"],"_links":{"self":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":0,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benbarefield.com\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}