I remember the first time I used WPF. It was a while ago. So long ago, if I am remembering correctly, that there was no text box control. No, I am not joking. Microsoft stated that the workaround was to simply create your own textbox using WPF because it was so easy and awesome. I distinctly remember thinking to myself, WTF? It got better, but I’ve always found WPF confusing and was really wanting to write a book about WPF so I could title it “WTF with WPF” — alas, it never came about.
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. I thought, you know, WPF is a LOT better at handling stuff like this, so I’ll just bite the bullet and port the code over to WPF. About 5 minutes in, I decided to add a menu to the form and was promptly greeted with the following:
WTF? Seriously? A menu is about as basic as it gets, and there it was floating off to the left of the window. It looked like some half-wit coder who had no idea what he was doing put it together. And although that aptly describes my skills with WPF, I was bothered none-the-less. After some searching, I found that this is actually by design, and it’s makes sense once you understand it. 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’t block the menu. Cool! Except I’m not using a tablet. And none of my other menus work this way.
I quickly found a number of “workarounds” but they all involved changing a windows setting to say you are left handed instead of right handed. This moves the menu back to the “normal” position, but it seems rather heavy handed to require your users to change a windows settings to get your application working correctly. And I think the instructions began by saying something like “go into the registry” which is, of course, exactly what we want all users doing. Because nobody has ever hosed a machine completely in the registry editor before. Anyway, a bit more searching resulted in this Stack Overflow question.
I took the liberty of pairing it down a bit into the following:
1 2 3 4 5 |
var menuDropAlignmentField = typeof(SystemParameters).GetField("_menuDropAlignment", BindingFlags.NonPublic | BindingFlags.Static); Action setAlignmentValue = () => { if (SystemParameters.MenuDropAlignment && menuDropAlignmentField != null) menuDropAlignmentField.SetValue(null, false);}; setAlignmentValue(); SystemParameters.StaticPropertyChanged += (sender, e) => { setAlignmentValue(); }; |
Although the example showed this code in the static constructor of the form, I don’t believe it has anything to do explicitly with the form itself. You should be able to run this anywhere and the menus in your application will start showing up like they normally do.
Load comments