简体   繁体   中英

What is the best way to change WPF layout when orientation changes?

My UI have to change with the orientation of the screen. Especially i have to change the grid.rows/columns-property and the allignment of my Controls. An animation of the position-change from my Controls would be nice but it doesn't have to. Much more important for me is that i have an short and clearly arranged code.

I have some approaches but may be there are some more or better ways.

  1. I create an extra View for each orientation-state. (This generates double Code - may be not very good, or does it?)
  2. Something like Storyboard where i can change the position of controls with XAML. (i don't know how to change grid.row and alignment with storyboard - is it actually possible?)
  3. Change it by C#-code. (but I think this is not a proper WPF-style?)

May be with an Example.

Thx Stefan

For example, you can use RotateTransform in order to rotate your view when the orientation changes.

Another approach: Since you were trying to work with Grid.Row and Grid.Column swapping, I'd rather recommend to use StackPanel , where you'd just need to set the Orientation dependency property to Horizontal or Vertical .

Note: You are right, I do not recommend either to use C# code in order to solve this, since it's a situation on the View side, not on the Model one. (Separation of Concerns.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM