wpf grid 的用法

WPF中的Grid是一种布局控件,可用于将子控件按照行和列的方式排列。

以下是Grid控件的用法:

  1. 在XAML文件中,添加一个Grid控件:
xml 复制代码
<Grid>
</Grid>
  1. 在Grid控件中,添加行和列定义:
xml 复制代码
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
</Grid>

在上面的例子中,Grid定义了两行和两列。行和列可以使用特定的值来定义大小,如"Auto"表示根据内容自动调整大小,"*"表示占用剩余空间的大小。

  1. 将子控件添加到Grid中,并设置其位置:
xml 复制代码
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    
    <Button Grid.Row="0" Grid.Column="0" Content="Button 1"/>
    <Button Grid.Row="0" Grid.Column="1" Content="Button 2"/>
    <Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Content="Button 3"/>
</Grid>

在上面的例子中,三个Button控件被添加到Grid中,并通过Grid.Row和Grid.Column属性来设置它们的位置。Grid.ColumnSpan属性可以指定控件跨越的列数。

通过Grid控件,可以很灵活地布局子控件,使其按照行和列的方式排列,以实现更复杂的布局效果。

1、功能最强大,布局最灵活的容器,将区域划分不同大小网格

2、主要属性配置:

基本属性:

行定义:RowDefinitions->RowDefinition(Height:*/auto/数字)

列定义:ColumnDefinitions->ColumnDefinition(Width:*/auto/数字)

尺寸共享:Grid.IsSharedSizeScope="True"配合行/列中的SharedSizeGroup属性

附加属性:Grid.Row/Grid.Column3

使用场景:无处不在c0

下面是我写的一段代码

cs 复制代码
<Window x:Class="kongjian.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:kongjian"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100">
            </RowDefinition>
            <RowDefinition>
            </RowDefinition>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Background="red">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Border Background="Green" Width="20" Height="10" Grid.Column="2" Grid.RowSpan="2"/>
            <GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="20" Background="red"/>
        </Grid>
        <Grid Grid.Row="1" Background="Green">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

        </Grid>
    </Grid>
</Window>

界面如下:

相关推荐
我好喜欢你~11 小时前
WPF---数据模版
wpf
hqwest1 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest2 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple2 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题3 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘3 天前
开发WPF项目时遇到的问题总结
wpf
hqwest4 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars4 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest5 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest5 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf