XAML 页面布局
程序员文章站
2022-06-08 14:58:26
...
1. Grid 布局(网格控件)
<Grid Margin="0,0,0,0" ShowGridLines="True">
<Grid.RowDefinitions><!--行设置-->
<!--每个RowDefinition表示一行-->
<RowDefinition MaxHeight="50"></RowDefinition>
<RowDefinition MaxHeight="50"></RowDefinition>
<RowDefinition MaxHeight="50"></RowDefinition>
<RowDefinition MaxHeight="50"></RowDefinition>
<RowDefinition MaxHeight="50"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions><!--列设置-->
<!--每个ColumnDefinition表示一列-->
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!-- 通过附加属性Grid.Row、Grid.Column指定位置-->
<TextBlock Text="姓名:" TextAlignment="Right" FontSize="16" Height="25" Grid.Row="1" Grid.Column="0"></TextBlock><!--默认位置为第一行第一列-->
<!--Grid.RowSpan="2" 合并单元格-->
</Grid>
补充:
如果想要一个属性中设置两个控件时,可以通过布局容器进行控制
<Button HorizontalAlignment="Left" Margin="219,316,0,0" VerticalAlignment="Top" Width="108" Height="49" Click="Button_Click">
<Button.Content>
<Grid Margin="0,0,0,0" Height="30" Width="100" ><!-- 包含两个控件 -->
<MediaElement Source="1.mp4" HorizontalAlignment="Left"></MediaElement>
<TextBlock Text="确认" HorizontalAlignment="Right"></TextBlock>
</Grid>
</Button.Content>
</Button>
2. StackPanel
把子控件横向或纵向排列,Orientation属性设置为Horizontal 为水平,Vertical 为垂直
<StackPanel Orientation="Horizontal">
<TextBlock Text="Hello" Height="30" Width="100"></TextBlock>
<TextBlock Text="Hello" Height="30" Width="100"></TextBlock>
<TextBlock Text="Hello" Height="30" Width="100"></TextBlock>
<TextBlock Text="Hello" Height="30" Width="100"></TextBlock>
<TextBlock Text="Hello" Height="30" Width="100"></TextBlock>
</StackPanel>
3. Canvas
根据坐标、大小进行绝对定位布局
<Canvas>
<TextBox Width="100" Height="30" Text="Hello" Canvas.Left="50" Canvas.Top="100"></TextBox>
</Canvas>
※通过代码控制附加属性:
SetValue、GetValue
<Canvas>
<TextBox Name="txtText" Width="100" Height="30" Text="Hello" Canvas.Left="50" Canvas.Top="100"></TextBox>
<Button Content="xxx" HorizontalAlignment="Left" Margin="219,316,0,0" VerticalAlignment="Top" Width="108" Height="49" Click="Button_Click_1"/>
</Canvas>
// 获取txtText的Canvas.Left属性
double d = (double)txtText.GetValue(Canvas.LeftProperty);
d += 10;
txtText.SetValue(Canvas.LeftProperty,d);
上一篇: iveiw table可编辑单元格
下一篇: NPOI设置单元格背景色