Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Orientation

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!

like image 536
Sydnal Avatar asked Nov 30 '11 08:11

Sydnal


People also ask

What is the difference between StackPanel and DockPanel?

StackPanel vs. For example, the order of child elements can affect their size in a DockPanel but not in a StackPanel. This is because StackPanel measures in the direction of stacking at PositiveInfinity, whereas DockPanel measures only the available size. The following example demonstrates this key difference.

What is the use of StackPanel in WPF?

A StackPanel allows you to stack elements in a specified direction. By using properties that are defined on StackPanel, content can flow both vertically, which is the default setting, or horizontally.

What is StackPanel?

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.


2 Answers

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
  }
}
like image 197
SvenG Avatar answered Oct 23 '22 19:10

SvenG


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.

like image 2
Brock Avatar answered Oct 23 '22 18:10

Brock