{"id":80305,"date":"2018-08-08T04:06:34","date_gmt":"2018-08-08T04:06:34","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=80305"},"modified":"2019-08-15T13:45:30","modified_gmt":"2019-08-15T13:45:30","slug":"wpf-menu-displays-to-the-left-of-the-window","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/wpf-menu-displays-to-the-left-of-the-window\/","title":{"rendered":"WPF Menu Displays to the Left of the Window"},"content":{"rendered":"<p>I remember the first time I used WPF.\u00a0 It was a while ago.\u00a0 So long ago, if I am remembering correctly, that there was no text box control.\u00a0 No, I am not joking.\u00a0 Microsoft stated that the workaround was to simply create your own textbox using WPF because it was so easy and awesome.\u00a0\u00a0I distinctly remember thinking to myself, WTF?\u00a0It got better, but I&#8217;ve always found WPF confusing and was really wanting to write a book about WPF so I could title it &#8220;WTF with WPF&#8221; &#8212; alas, it never came about.\u00a0\u00a0<\/p>\n<p>The other day I found myself making a tool using a WinForms app as the UI, but I ran into an issue with dynamically adding controls to a scrolling panel.\u00a0 I thought, you know, WPF is a LOT better at handling stuff like this, so I&#8217;ll just bite the bullet and port the code over to WPF.\u00a0 About 5 minutes in, I decided to add a menu to the form and was promptly greeted with the following:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/08\/Damon.Wpf_.Menu_.01.png\" alt=\"WPF Menu to the Left of Window\" width=\"651\" height=\"392\" \/><\/p>\n<p>WTF?\u00a0 Seriously?\u00a0 A menu is about as basic as it gets, and there it was floating off to the left of the window.\u00a0 It looked like some half-wit coder who had no idea what he was doing put it together.\u00a0 And although that aptly describes my skills with WPF, I was bothered none-the-less.\u00a0 After some searching, I found that this is actually by design, and it&#8217;s makes sense once you understand it.\u00a0 If you are using a tablet, and you are right handed, the menu appears to the left of where you touched so your hand doesn&#8217;t block the menu.\u00a0 Cool!\u00a0 Except I&#8217;m not using a tablet.\u00a0 And none of my other menus work this way.\u00a0\u00a0<\/p>\n<p>I quickly found a number of &#8220;workarounds&#8221; but they all involved changing a windows setting to say you are left handed instead of right handed.\u00a0 This moves the menu back to the &#8220;normal&#8221; position, but it seems rather heavy handed to require your users to change a windows settings to get your application working correctly.\u00a0\u00a0And I think the instructions began by saying something like &#8220;go into the registry&#8221; which is, of course, exactly what we want all users doing.\u00a0 Because nobody has ever hosed a machine completely in the registry editor before.\u00a0 Anyway, a bit more searching resulted in this <a href=\"https:\/\/stackoverflow.com\/questions\/37326546\/why-are-my-menus-opening-right-to-left-instead-of-left-to-right\">Stack Overflow question<\/a>.<\/p>\n<p>I took the liberty of pairing it down a bit into the following:<\/p>\n<p><code><\/code><\/p>\n<pre class=\"\">var\u00a0menuDropAlignmentField\u00a0=\u00a0typeof(SystemParameters).GetField(\"_menuDropAlignment\",\u00a0BindingFlags.NonPublic\u00a0|\u00a0BindingFlags.Static);\r\nAction\u00a0setAlignmentValue\u00a0=\u00a0()\u00a0=&gt;\u00a0{\r\nif\u00a0(SystemParameters.MenuDropAlignment\u00a0&amp;&amp;\u00a0menuDropAlignmentField\u00a0!=\u00a0null) menuDropAlignmentField.SetValue(null,\u00a0false);};\r\nsetAlignmentValue();\r\nSystemParameters.StaticPropertyChanged\u00a0+=\u00a0(sender,\u00a0e)\u00a0=&gt;\u00a0{\u00a0setAlignmentValue();\u00a0};<\/pre>\n<p><code><\/code><\/p>\n<p>Although the example showed this code in the static constructor of the form, I don&#8217;t believe it has anything to do explicitly with the form itself.\u00a0 You should be able to run this anywhere and the menus in your application will start showing up like they normally do.\u00a0<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I remember the first time I used WPF.\u00a0 It was a while ago.\u00a0 So long ago, if I am remembering correctly, that there was no text box control.\u00a0 No, I am not joking.\u00a0 Microsoft stated that the workaround was to simply create your own textbox using WPF because it was so easy and awesome.\u00a0\u00a0I distinctly&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[7575],"class_list":["post-80305","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/80305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=80305"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/80305\/revisions"}],"predecessor-version":[{"id":80308,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/80305\/revisions\/80308"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=80305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=80305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=80305"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=80305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}