WPF Orientation WPF Orientation wpf wpf

WPF Orientation


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:

Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);}void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e){  if (SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight)  {    // runs in landscape  }  else  {    // runs in portrait  }}


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:

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 you Main Window.xaml:

<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>

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'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