WPF中Grid布局XMAl与后台更改,最普通的登录界面为例。
<Grid Width="200" Height="100" >
<!--定义了两列-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions>
<!--定义了四行-->
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" TextAlignment="Right" Text="用户名:" Name="UserName"/>
<!--第一行第一列-->
<TextBlock Grid.Row="1" Grid.Column="0" TextAlignment="Right" Text="密码:" Name="pass"/>
<TextBlock Grid.Row="2" Grid.Column="0" TextAlignment="Right" Text="确认密码:" Name="repass"/>
<Button Grid.Row="3" Grid.ColumnSpan="2" Height="23" Width="100" Content="确认" Click="Button_Click" Name="btn"/>
<!--Grid.ColumnSpan="2":合并单元格-->
<TextBox Name="TxtBoxName" Grid.Row="0" Grid.Column="1" Text="" />
<!--第一行第二列-->
<PasswordBox Name="pass1" Grid.Row="1" Grid.Column="1"/>
<!--密码框-->
<PasswordBox Name="pass2" Grid.Row="2" Grid.Column="1" />
</Grid>
效果如图所示:
后台更改:
Grid.SetRow(UserName, 1);
Grid.SetColumn(UserName, 0);
Grid.SetRow(pass, 0);
Grid.SetColumn(pass, 0);
Grid.SetRow(repass, 2);
Grid.SetColumn(repass, 0);
Grid.SetRow(btn, 3);
Grid.SetColumnSpan(btn, 2);