C#windows编程:XAML控件布局

Border控件:内含一个子对象,子对象充满整个Border控件

  1. <Border DockPanel.Dock="Top" Padding="10" Margin="5"
  2.     Background="Aquamarine" Height="45">
  3.             <Label>1) DockPanel.Dock="Top"</Label>
  4.  </Border>

包含一个子对象Label.

Canvas控件:完全自由的对子控件位置安排   公共属性Canvas.Left    Canvas.Right    Canvas.Top    Canvas.Bottom

  1.     <Canvas Background="AliceBlue">
  2.         <Rectangle Canvas.Left="50" Canvas.Top="50" Height="40" Width="100"
  3.     Stroke="Black" Fill="Chocolate" />
  4.         <Rectangle Canvas.Right="50" Canvas.Bottom="50" Height="40" Width="100"
  5.     Stroke="Black" Fill="Bisque" />
  6.     </Canvas>

包含两个子对象矩形

DockPanel控件:允许将子控件贴靠到边上   公共属性DockPanel.Dock  {Left   Right   Top   Bottom}

  1.  <DockPanel Background="AliceBlue">
  2.         <Border DockPanel.Dock="Top" Padding="10" Margin="5"
  3.     Background="Aquamarine" Height="45">
  4.             <Label>1) DockPanel.Dock="Top"</Label>
  5.         </Border>
  6.         <Border DockPanel.Dock="Top" Padding="10" Margin="5"
  7.     Background="PaleVioletRed" Height="45" Width="200">
  8.             <Label>2) DockPanel.Dock="Top"</Label>
  9.         </Border>
  10.         <Border DockPanel.Dock="Left" Padding="10" Margin="5"
  11.     Background="Bisque" Width="200">
  12.             <Label>3) DockPanel.Dock="Left"</Label>
  13.         </Border>
  14.         <Border DockPanel.Dock="Bottom" Padding="10" Margin="5"
  15.     Background="Ivory" Width="200" HorizontalAlignment="Right">
  16.             <Label>4) DockPanel.Dock="Bottom"</Label>
  17.         </Border>
  18.         <Border Padding="10" Margin="5" Background="BlueViolet">
  19.             <Label Foreground="White">5) Last control</Label>
  20.         </Border>
  21. </DockPanel>

包含5个子对象Border

StackPanel控件:精简版DockPanel.以列表方式显示子控件

  1.  <StackPanel HorizontalAlignment="Left" Height="128" VerticalAlignment="Top"
  2.  Width="284" Margin="0,128,0,0" Orientation="Vertical">
  3.             <Button Content="Button" HorizontalAlignment="Left" Width="284"/>
  4.             <Button Content="Button" HorizontalAlignment="Left" Width="284"/>
  5.             <Button Content="Button" HorizontalAlignment="Left" Width="284"/>
  6.  </StackPanel>

排列三个button子对象

WrapPanel控件:扩展版StackPanel,容纳不了的子控件会被显示在下一行或下一列

  1.  <WrapPanel Background="AliceBlue">
  2.         <Rectangle Fill="#FF000000" Height="50" Width="50" Stroke="Black"
  3.     RadiusX="10" RadiusY="10" />
  4.         <Rectangle Fill="#FF111111" Height="50" Width="50" Stroke="Black"
  5.     RadiusX="10" RadiusY="10" />
  6.         <Rectangle Fill="#FF222222" Height="50" Width="50" Stroke="Black"
  7.     RadiusX="10" RadiusY="10" />
  8.         <Rectangle Fill="#FFFFFFFF" Height="50" Width="50" Stroke="Black"
  9.     RadiusX="10" RadiusY="10" />
  10.     </WrapPanel>

包含四个子对象矩形,可以更改窗口大小,观看排列效果

Grid控件:可以分为多行,多列显示子控件

  1. <Grid.RowDefinitions>                                                 //划分Grid为2行
  2.       <RowDefinition Height="109*"/>
  3.       <RowDefinition Height="210*"/>
  4.     </Grid.RowDefinitions>
  5.     <Grid.ColumnDefinitions>                                          //划分Grid为2列
  6.       <ColumnDefinition Width="191*"/>
  7.       <ColumnDefinition Width="326*"/>
  8.     </Grid.ColumnDefinitions>

多行多列属性。

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/86368044