I'm developing an application on a tablet with portrait orientation.
However, when the tablet is turned to landscape mode, the application also turns, and all the alignments are thrown off. So is there any way I can lock my WPF application to a single orientation?
Thank you!
Sven does a good job showing how to detect Orientation change...
However if you aren't writing a Metro app (where you can set preferred orientations in the manifest) there is no real way to NOT let the Orientation change, however if you are interested in only allowing Portrait you could do something like this:
View Model:
In you Main Window.xaml:
So we really aren't limiting the Orientation, we are just noticing when it happens, and re rotating our UI so it still looks like it is in Portrait mode :) Again this is mostly for non Metro Win 8 applications and or applications that also run on Win 7 tablets.
I have to agree with Martin: I have developed Tablet PC Apps myself and you should rather provide a layout that works well in landscape and portrait.
Besides from that you can detect the change in orientation this way:
I've came across the same issue when developing a wpf app for tablets and found this article from msdn explaining how to detect the screen rotation and orientation: http://msdn.microsoft.com/en-us/library/ms812142.aspx
I'm not aware of any public API to lock the screen orientation. Mostly because typically, Tablet PC manufacturers provided their own preinstalled utilities or drivers that used accelerometer data to change orientation. It was not a built in OS function. This may be changing in Windows 8.
While it's not the same as locking orientation, you can try reacting to orientation changes by adding a rotate transform to your root container's LayoutTransform. This would change the layout space so that your application thought it was still rotated 90 degrees but the rest of the OS would not agree. Therefore it's really only practical for full screen applications.