WPF容器使用

名称内容
Grid常用容器
StackPanel
容器,局部容器,可垂直水平排列
DockPanel
DockPanel容器,可以停靠四周
WrapPanel
WrapPanel容器,自适应,可根据 控件大小,适应排列 
UniformGridUniformGrid容器,在有限的空间内均分空间

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Border Grid.Row="0" Background="Blue" />
        <Border Grid.Row="1" Background="Red"/>
        <Border Grid.Row="0" Grid.Column="1" Background="Green"/>
        <Border Grid.Row="1" Grid.Column="1" Background="Yellow"/>

        <!--StackPanel容器,局部容器,可垂直水平排列,-->
        <StackPanel Orientation="Horizontal">
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>

        </StackPanel>
        <!--WrapPanel容器,自适应,可根据 控件大小,适应排列 -->
        <WrapPanel Grid.Column="1" Orientation="Vertical">
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
        </WrapPanel>
        <!--DockPanel容器,可以停靠四周 -->
        <DockPanel Grid.Row="1" LastChildFill="False">
            <Button Width="100" Height="50" DockPanel.Dock="Left"></Button>
            <Button Width="100" Height="50" DockPanel.Dock="Right"></Button>
            <Button Width="100" Height="50" DockPanel.Dock="Top"></Button>
            <Button Width="100" Height="50" DockPanel.Dock="Bottom"></Button>
        </DockPanel>
        <!--UniformGrid容器,在有限的空间内均分空间 -->
        <UniformGrid Grid.Row="1" Grid.Column="1" Rows="3" Columns="3">
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
            <Button Width="100" Height="50"></Button>
        </UniformGrid>
    </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、付费专栏及课程。

余额充值