WPF布局

参考链接:https://blog.csdn.net/weixin_43437202/article/details/88292620

Grid                    按行列排列内容,没有特别说明行列,则内容充满容器
StackPanel        水平或垂直的放置内容元素
WrapPanel         自动换行的方式放置内容元素
DockPanel         按照Top、left、right等Dock属性布局内容元素,最后一个元素充满可利用的容器空间

UniformGrid     均分容器空间
Canvas             在内容元素中设置附加属性Top、Left、Right、Botton根据附件属性的值布局类似Winform坐标,一般很少用,用stackpanel 和Grid代替,除非这个窗口界面以后布局不会改变,大小也不会改变。

Grid和StackPanel的区别:

1、Grid相当于表格,几行几列都可以;
2、StackPanel相当于一行横排或者一列竖排,嵌套的话也能实现Grid效果。

3、Grid是以表格的方式定位子元素。可以定义行和列,然后将元素布局到表格当中
4、StackPanel是以水平或者竖直方向对子元素进行排列。

Grid:
1、Grid本身并不占用任何空间,可以看作是用单元格对父控件空间的划分;
2、会根据不同分辨率自动响Resize
3、可以合并单元格(Grid.RowSpan / Grid.ColumnSpan)如果整个页面的布局都用Grid的话,可读性会降低。

4、如果水平或垂直方向存在多个Grid,在Grid中添加控件时,如果没有指定Grid.Row或者Grid.Column,添加的控件默认放在Grid.Row="0" Grid.Column="0"中。

 WrapPanel:
从左至右按顺序位置定位子元素,如果排满断开至下一行。
后续排序按照从上至下或从右至左的顺序进行。
WrapPanel面板也提供了Orientation属性设置排列方式,这跟StatickPanel基本相似。不同的是WrapPanel会根据内容自动换行。

StackPanel:

        顺序对它的子元素进行排列。StackPanel的功能最简单,只支持将子控件按书写顺序竖排(Orientation=“Vertical” 默认)或者横排(Orientation=“Horizontal”)。

<Window x:Class="_01Wpf布局基础.StackPanel"
        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:_01Wpf布局基础"
        mc:Ignorable="d"
        Title="StackPanel" Height="300" Width="600">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition ></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        
        <!--当Panel中控件排列不了时不会自动换行-->
        <StackPanel  Grid.Row="0" Orientation="Vertical" Background="Yellow">
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
        </StackPanel>

        <!--当Panel中控件排列不了时会自动换行-->
        <WrapPanel Grid.Row="1" Orientation="Horizontal" Background="GreenYellow">
            <Button Width="96" Height="20"/>
            <Button Width="96" Height="20"/>
            <Button Width="96" Height="20"/>
            <Button Width="100" Height="20" />
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
            <Button Width="100" Height="20"/>
        </WrapPanel>

        <!--以最后一个元素填充满所有空间,类似Winform控件Dock属性-->
        <DockPanel Grid.Row="0" Grid.Column="1" LastChildFill="true" Background="BlueViolet"><!--设置最后一个元素不填充满所有空间-->
            <Button Width="50" Height="20" DockPanel.Dock="Left"/>
            <Button Width="50" Height="20" DockPanel.Dock="Bottom"/>
            <Button Width="50" Height="20" DockPanel.Dock="Right"/>
            <Button Width="50" Height="20" DockPanel.Dock="Top"/>
            <Button></Button>
        </DockPanel>

        <!--在有效的空间内均分容器空间-->
        <UniformGrid  Grid.Row="1" Grid.Column="1" Rows="3" Columns="3" Background="AliceBlue"> <!--均分成3行3列-->
            <Button Margin="1"/>
            <Button Margin="1"/>
            <Button Margin="1"/>
            <Button/>
            <Button/>
            <Button/>
            <Button/>
            <Button/>
            <Button/>
        </UniformGrid>
    </Grid>
</Window>

Canvas布局 

<Window x:Class="_01Wpf布局基础.Canvas窗体"
        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:_01Wpf布局基础"
        mc:Ignorable="d"
        Title="Canvas窗体" Height="450" Width="800">
    <Canvas>
        <TextBlock Text="用户名:" Canvas.Left="12" Canvas.Top="12"/>
        <TextBox Height="23" Width="200" BorderBrush="Black" Canvas.Left="66" Canvas.Top="9"/>
        <TextBlock Text="密码:" Canvas.Left="12" Canvas.Top="40.72" Height="16" Width="36"/>
        <TextBox Height="23" Width="200" BorderBrush="Black" Canvas.Left="66" Canvas.Top="38"/>
        <Button Content="确定" Width="80" Height="22" Canvas.Left="100" Canvas.Top="67"/>
        <Button Content="清除" Width="80" Height="22" Canvas.Left="186" Canvas.Top="67"/>
        
    </Canvas>
</Window>



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值