WPF容器

Grid容器

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
		
        <Grid.RowDefinitions>
            <!--  Height="2*":当前行是其它行的2-->
            <!--  Height="auto":当前行为自适应高度,如果没有指定高度,当前行为0 -->
            <!--  Height="100":当前行为设定高度-->
            <RowDefinition Height="2*"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        
         <!-- Grid.RowSpan="2":占用多行,从第1行开始-->
        <Border Background="Blue" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" ></Border>
		<!-- Grid.ColumnSpan="2":占用多列,从第1列开始-->
        <Border Background="Green"  Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"></Border>
        <Border Background="Yellow"  Grid.Row="1"  Grid.Column="0"></Border>
        <Border Background="Red"  Grid.Row="1" Grid.Column="1" ></Border>
</Grid>

在这里插入图片描述

StackPanel容器

StackPanel :默认垂直方向排列,只能一个方向排列,排不下不会换行

<StackPanel Orientation="Horizontal">
        <Button Background="Blue" Height="100" Width="120"></Button>
        <Button Background="Blue" Height="100" Width="120"></Button>
        <Button Background="Blue" Height="100" Width="120"></Button>
        <Button Background="Blue" Height="100" Width="120"></Button>
        <Button Background="Blue" Height="100" Width="120"></Button>
</StackPanel>

在这里插入图片描述

WrapPanel容器

WrapPanel: 默认水平方向排列,排不下会换一行排列

<WrapPanel Orientation="Horizontal">
        <Button Height="100" Width="120"></Button>
        <Button Height="100" Width="120"></Button>
        <Button Height="100" Width="120"></Button>
        <Button Height="100" Width="120"></Button>
        <Button Height="100" Width="120"></Button>
</WrapPanel>

在这里插入图片描述

DockPanel容器

LastChildFill:最后一个元素是否填充剩余空间

<DockPanel LastChildFill="False" >
        <!--DockPanel.Dock:该属性继承自DcokPanel,只有在DockPanel容器里才有这个属性,其它标签也有类似属性 -->
        <!--DockPanel.Dock:设置控件的停靠位置 -->
        <Button Height="100" Width="80" DockPanel.Dock="Bottom"></Button>
        <Button Height="100" Width="80" DockPanel.Dock="Top"></Button>
        <Button Height="100" Width="80" DockPanel.Dock="Left"></Button>
        <Button Height="100" Width="80" DockPanel.Dock="Right"></Button>
</DockPanel>

在这里插入图片描述

案例

<!-- 第一层Grid-->
<Grid>
	<!-- 21-->
	<Grid.RowDefinitions>
		<RowDefinition Height="100"></RowDefinition>
		<RowDefinition></RowDefinition>
	</Grid.RowDefinitions>
	<Grid.ColumnDefinitions>
		<ColumnDefinition></ColumnDefinition>
	</Grid.ColumnDefinitions>
	<!-- 标题头部 -->
	<Grid Grid.Row="0" Grid.Column="0">
		<Border Background="Blue"></Border>
	</Grid>

	<!-- Body内容体 -->
	<Grid Grid.Row="1" Grid.Column="0">
		<Grid.RowDefinitions>
			<RowDefinition></RowDefinition>
		</Grid.RowDefinitions>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="100"></ColumnDefinition>
			<ColumnDefinition></ColumnDefinition>
		</Grid.ColumnDefinitions>
		<!-- 左边导航栏 -->
		<Grid Grid.Row="0" Grid.Column="0">
			<Border Background="LightBlue" ></Border>
		</Grid>

		<!-- 右边内容体 -->
		<Grid Grid.Row="0" Grid.Column="1">
			<Grid.RowDefinitions>
				<RowDefinition Height="0.8*"></RowDefinition>
				<RowDefinition></RowDefinition>
				<RowDefinition></RowDefinition>
			</Grid.RowDefinitions>
			<Grid.ColumnDefinitions>
				<ColumnDefinition></ColumnDefinition>
				<ColumnDefinition></ColumnDefinition>
				<ColumnDefinition></ColumnDefinition>
				<ColumnDefinition></ColumnDefinition>
				<ColumnDefinition></ColumnDefinition>
			</Grid.ColumnDefinitions>
			<!-- 第一行 5个小方格 -->
			<Border Background="Gray" Grid.Row="0" Grid.Column="0"></Border>
			<Border Background="LightCoral" Grid.Row="0" Grid.Column="1"></Border>
			<Border Background="LightCyan" Grid.Row="0" Grid.Column="2"></Border>
			<Border Background="LightGray" Grid.Row="0" Grid.Column="3"></Border>
			<Border Background="LightBlue" Grid.Row="0" Grid.Column="4"></Border>
			<!-- 第二行  -->
			<Border Background="LightBlue" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"></Border>
			<Border Background="LightCoral" Grid.Row="1" Grid.Column="3" Grid.ColumnSpan="2"></Border>

			<!-- 第三行  -->
			<Border Background="Gray" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3"></Border>
			<Border Background="LightGray" Grid.Row="2" Grid.Column="3" Grid.ColumnSpan="2"></Border>
		</Grid>
	</Grid>
</Grid>

在这里插入图片描述

### WPF 中 Grid 容器的使用教程 #### 1. Grid 的概述 Grid 是 WPF 中一种强大的布局控件,允许开发者通过定义行和列的方式构建复杂的用户界面。其灵活性使得它可以适应多种场景下的 UI 设计需求[^2]。 #### 2. 创建简单的 Grid 布局 下面是一个基础的例子,展示如何创建一个具有两行两列的网格: ```xml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Grid Example" Height="350" Width="525"> <Grid> <!-- 定义行列 --> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="2*" /> </Grid.ColumnDefinitions> <!-- 添加子元素并指定位置 --> <TextBlock Text="Top Left" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center"/> <TextBlock Text="Top Right" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"/> <Button Content="Bottom Spanning Both Columns" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center" Margin="10"/> </Grid> </Window> ``` 在这个例子中: - `RowDefinitions` 和 `ColumnDefinitions` 被用来定义网格的结构。 - 子元素可以通过设置 `Grid.Row` 和 `Grid.Column` 属性来放置到特定的位置上。 - 可以利用 `Grid.RowSpan` 或者 `Grid.ColumnSpan` 让某个控件跨越多行或多列[^1]。 #### 3. 关键属性详解 以下是几个重要的 Grid 属性及其作用说明: - **RowDefinitions / ColumnDefinitions**: 这两个集合用于定义网格中的行数和列数以及每行或列的高度宽度比例。 - **Height / Width**: 行高或者列宽可以被设定为绝对值、相对星号(`*`)表示的比例或者是自动调整大小 (`Auto`)。 - **HorizontalAlignment / VerticalAlignment**: 设置子控件在其单元格内的水平垂直对齐方式。 - **Margin / Padding**: 边距控制子项之间的间距;填充则影响内部空间。 这些属性共同决定了最终呈现出来的视觉效果。 #### 4. 实际应用案例分析 假设我们需要设计一款联系人管理应用程序,在其中有一个窗口显示多个字段的信息(姓名、电话号码等)。我们可以采用如下策略实现这一目标: ```xml <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <!-- Label Row --> <RowDefinition Height="Auto"/> <!-- TextBox Row --> <RowDefinition Height="Auto"/> <!-- Button Row --> </Grid.RowDefinitions> <Label Content="Name:" Grid.Row="0"/> <TextBox Name="txtName" Grid.Row="1"/> <StackPanel Orientation="Horizontal" Grid.Row="2"> <Button Content="Save" Click="OnSaveClick"/> <Button Content="Cancel" Click="OnCancelClick" Margin="10,0,0,0"/> </StackPanel> </Grid> ``` 此代码片段展示了如何组合不同类型的控件形成完整的表单区域,并合理分配各部分所占的空间[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值