导航

WPF Layout

Posted on 2010-03-11 13:48  AlexCube  阅读(359)  评论(0编辑  收藏  举报

常用的有5种 Layout panels:

  • Grid Panel
  • Stack Panel
  • Dock Panel
  • Wrap Panel
  • Canvas Panel

技巧:

  • 避免使用固定位置 - 多使用Alignment 和 Margin 属性
  • 不要使用固定大小 - 尽可能将Width和Height设置为Auto
  • 不要滥用Canvas Panel, 他只能用于矢量图片
  • 在对话框中使用StackPanel 布置 按钮控件Buttons
  • Use a GridPanel to layout a static data entry form. Create a Auto sized column for the labels and a Star sized column for the TextBoxes.
  • Use an ItemControl with a grid panel in a DataTemplate to layout dynamic key value lists. Use the SharedSize feature to synchronize the label widths.

Vertical 和 Horizontal Alignment 属性

 

v2_alignment

Margin 和 Padding

用于为控件预留空间

  • Margin - 控件外部预留空间
  • Padding - 控件内部预留空间
  • 外面控件的Padding相当于内部控件的Margin

padding_margin

Height 和 Width 属性

不推荐使用,可以用于设置控件的固定大小。最好用MinHeight,MaxHeight,MinWidth 和MaxWidth属性定义可接受的大小范围

 

Overflow Handling

Clipping

Layout panels typically clip those parts of child elements that overlap the border of the panel. This behavior can be controlled by setting the ClipToBounds property to true or false.

cliptobounds

 

Scrolling

When the content is too big to fit the available size, you can wrap it into a ScrollViewer. The ScrollViewer uses two scroll bars to choose the visible area.

The visibility of the scrollbars can be controlled by the vertical and horizontal ScrollbarVisibility properties.

<ScrollViewer>
    <StackPanel>
        <Button Content="First Item" />
        <Button Content="Second Item" />
        <Button Content="Third Item" />
    </StackPanel>
</ScrollViewer>

 

From: http://www.wpftutorial.net/LayoutProperties.html