Sven does an excellent job of detecting a change in orientation ...
However, if you are not writing a Metro application (where you can set preferred orientations in the manifest), there is no real way to NOT allow orientation changes, however, if you are only interested in letting the portrait go, you could do something like this:
View Model:
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged); } public bool IsLandscape { get; set; } void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) { if (SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight) { IsLandscape = true; } else { IsLandscape = false; } RaisePropertyChanged( "IsLandscape" ); }
In the main .xaml window:
<Border > <Border.Style> <Style TargetType="{x:Type Border}"> <Style.Triggers> <DataTrigger Binding="{Binding IsLandscape}" Value="True"> <Setter Property="LayoutTransform"> <Setter.Value> <RotateTransform Angle="90"/> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style> </Border.Style> ///The rest of your controls and UI </Border>
Thus, we really do not limit Orientation, we just notice when this happens, and rotate our user interface so that it still looks like in portrait mode :) Again, this is mainly for applications that do not support Metro Win 8, and applications that also run on Win 7 tablets.
Brock source share