1、布局容器Grid、StackPanel、GroupBox、DockPanel、WrapPanel

本文深入讲解了WPF中各种布局控件的使用方法,包括Grid网格布局、StackPanel堆叠面板、DockPanel停靠面板、WrapPanel自动换行面板及GroupBox的特性与应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Grid——网格布局,其中控件或容器需指定位置

StackPanel——堆叠面板,其中的控件水平布局、竖直布局

DockPanel——停靠面板,内部控件或容器可以放置在上、下、左、右

WrapPanel——可以看作是具有自动换行功能的StackPanel容器。窗体太小时,其末尾的控件会自动换行。像Java中的流布局

布局原则:先整体规划(Grid),再局部规划(Grid、StackPanel等)

如下图,Grid有5行3列,具体布局、控件查看文档大纲

 

xaml代码

<Window x:Class="DemoWPF61.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:DemoWPF61"
        mc:Ignorable="d"
        Title="MainWindow" Height="240" Width="350">
    <Grid >
        <!--行定义,5行-->
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="30"/>
            <!--剩余高度-->
            <RowDefinition Height="*"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <!--列定义,3列-->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <!--网格的2,3两列放置StackPanel-->
        <Grid Grid.Column="1" Grid.ColumnSpan="2">
            <!--水平布局,右对齐-->
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <!--方式1-->
                <Button Content="夕西行" Margin="5,0,0,0"/>
                <!--方式2-->
                <Button Margin="5,0,5,0">我的博客</Button>
            </StackPanel>
        </Grid>
        <!--网格的1列2行放置Image,默认居中对齐-->
        <Grid Grid.Column="0" Grid.Row="1">
            <Image Source="C:/Users/Jv/Desktop/lena512.tiff"/>
        </Grid>
        <!--网格的1~3列放置StackPanel-->
        <Grid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="2">
            <!--水平布局,默认左对齐-->
            <StackPanel Orientation="Horizontal">
                <Button Margin="5,0,0,0">园子</Button>
                <Button Margin="5,0,0,0">新闻</Button>
                <Button Margin="5,0,0,0">博问</Button>
            </StackPanel>
        </Grid>
        <Grid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="3">
            <!--左右居中,上下居中-->
            <Label Content="第4行,占三列" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
        <Grid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="4">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Button Margin="5,0,0,0">关于我们</Button>
                <Button Margin="5,0,0,0">联系我们</Button>
                <Button Margin="5,0,0,0">广告服务</Button>
                <Button Margin="5,0,0,0">人才服务</Button>
                <Button Margin="5,0,0,0">版权</Button>
            </StackPanel>
        </Grid>
    </Grid>
</Window>

【GroupBox】GroupBox内只能有一个元素,可用StackPanel承载多个控件

布局、控件如图所示 

  

<Window x:Class="DemoWPF61.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:DemoWPF61"
        mc:Ignorable="d"
        Title="MainWindow" Height="190" Width="200">
    <!--StackPanel纵向布局,纵向对齐方式:底对齐-->
    <StackPanel Orientation="Vertical" VerticalAlignment="Bottom" >
        <!--GroupBox内只能有一个元素,StackPanel来承载更多的控件-->
        <GroupBox Header="网站分类">
            <!--StackPanel内,纵向布局-->
            <StackPanel Orientation="Vertical">
                <Button Content=".NET技术(16)"/>
                <Button Content="编程语言(13)"/>
            </StackPanel>
        </GroupBox>
        <GroupBox Header="链接">
            <StackPanel Orientation="Vertical">
                <Button Content="反馈和建议"/>
                <Button Content="官方博客"/>
            </StackPanel>
        </GroupBox>
    </StackPanel>
</Window>

 【DockPanel】

  

<Window x:Class="DemoWPF61.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:DemoWPF61"
        mc:Ignorable="d"
        Title="MainWindow" Height="150" Width="230">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" >
            <Label Content="StackPanel停靠在DockPanel的上边"/>
        </StackPanel>
        <StackPanel DockPanel.Dock="Bottom" Height="20">
        </StackPanel>
        <StackPanel DockPanel.Dock="Left" Width="30">
        </StackPanel>
        <StackPanel DockPanel.Dock="Right" Width="30"/>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Label Content=" 中间地带,是我的天下" />
        </StackPanel>
    </DockPanel>
</Window>

 【WrapPanel】可以看作是具有自动换行功能的StackPanel容器。默认从左到右排列。

   

左图最后一个Button的高度会变成最小尺寸。Orientation="Vertical"得到右图(默认水平布局)

<Window x:Class="DemoWPF61.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:DemoWPF61"
        mc:Ignorable="d"
        Title="MainWindow" Height="150" Width="209.199">
    <WrapPanel Margin="3">
        <Button Content="Top" VerticalAlignment="Top"/>
        <Button Content="Bottom" VerticalAlignment="Bottom"/>
        <Button Content="靠我撑大" MinHeight="40"/>
        <Button Content="Center" VerticalAlignment="Center"/>
        <Button Content="无对齐方式"/>
    </WrapPanel>
</Window>

 

转载于:https://www.cnblogs.com/xixixing/p/10959455.html

帮我把所有的框框改圆角<Window x:Class="O_S_ATE.View.ConfigCBIP" 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:O_S_ATE.View" mc:Ignorable="d" Title="配置通信板IP" Height="500" Width="720" Loaded="Window_Loaded" Closing="Window_Closing" WindowStartupLocation="CenterScreen"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="180" /> <RowDefinition Height="1*" /> </Grid.RowDefinitions> <Grid Grid.Row="0" > <Grid.ColumnDefinitions> <ColumnDefinition Width="1*" /> <ColumnDefinition Width="1*" /> </Grid.ColumnDefinitions> <GroupBox Grid.Column="0"> <GroupBox.Header> <Label Content="配置IP地址" /> </GroupBox.Header> <Grid> <Grid.RowDefinitions> <RowDefinition Height="1*" /> <RowDefinition Height="1*" /> <RowDefinition Height="1*" /> <RowDefinition Height="1*" /> <RowDefinition Height="1*" /> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Name="TxtBlock_SetIPNumbers" Text="已设置IP地址数: 0" VerticalAlignment="Center" FontSize="20" /> <DockPanel Grid.Row="1" VerticalAlignment="Center"> <Label Content="IP地址:" Width="60" /> <TextBox Name="TxtBox_IPAddress" VerticalAlignment="Center" Text="192.168.1.101" /> </DockPanel> <DockPanel Grid.Row="2" VerticalAlignment="Center"> <Label Content="子网掩码:" Width="60" /> <TextBox Name="TxtBox_Mask" VerticalAlignment="Center" Text="255.255.255.0"/> </DockPanel> <DockPanel Grid.Row="3" VerticalAlignment="Center"> <Label Content="网关:" Width="60"/> <TextBox Name="TxtBox_Gateway" VerticalAlignment="Center"/> </DockPanel> <Grid Grid.Row="4"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*" /> <ColumnDefinition Width="1*" /> </Grid.ColumnDefinitions> <DockPanel Grid.Column="0" VerticalAlignment="Center"> <Label Content="板卡ID:" Width="60"/> <TextBox Name="Txtbox_BoardID" VerticalAlignment="Center" Text="0001" /> </DockPanel> <StackPanel Grid.Column="1" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Right"> <Label Name="Lbl_Tipinfo"/> <Button Name="Btn_SettingIP" Content="设置" Margin="2,0,2,0" FontSize="16" Click="Btn_SettingIP_Click" Style="{StaticResource RoundButtonStyle}" Width="60" Foreground="White"/> </StackPanel> </Grid> </Grid> </GroupBox> <Grid Grid.Column="1" > <GroupBox > <GroupBox.Header> <Label Content="已配置IP地址列表" /> </GroupBox.Header> <ListBox Name="Listbox_AlreadySetIPAddress"> </ListBox> </GroupBox> </Grid> </Grid> <Grid Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition Height="1*" /> <RowDefinition Height="52" /> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="2*" /> <ColumnDefinition Width="4*" /> </Grid.ColumnDefinitions> <GroupBox Grid.Column="0"> <GroupBox.Header> <Label Content="本机IP地址信息" /> </GroupBox.Header> <ListBox Name="Listbox_LocalIPAddress"> </ListBox> </GroupBox> <GroupBox Grid.Column="1"> <GroupBox.Header> <Label Content="信息" /> </GroupBox.Header> <RichTextBox x:Name="RTxtbox_Info" IsReadOnly="True"> </RichTextBox> </GroupBox> </Grid> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Name="Btn_GetThisComputerIPAddress" Content="刷新本机IP地址" Margin="10,10,10,10" Width="100" Foreground="White" Click="Btn_GetThisComputerIPAddress_Click" Style="{StaticResource RoundButtonStyle}"/> <Button Name="Btn_GetNetworkConfiguration" Content="获取本机网络配置" Margin="10,10,10,10" Width="120" Foreground="White" Click="Btn_GetNetworkConfiguration_Click" Style="{StaticResource RoundButtonStyle}"/> <Button Name="Btn_GetBoardInfo" Content="获取板卡信息" Margin="10,10,10,10" Width="100" Foreground="White" Click="Btn_GetBoardInfo_Click" Style="{StaticResource RoundButtonStyle}"/> </StackPanel> </Grid> </Grid> </Window>
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值