wpf之StackPanel布局

注意两个参数:

Orientation :控制排列是水平方向,还是垂直方向(Horizontal 水平方向   Vertical垂直方向)

FlowDirection:控制控件排序是从右往左还是从左往右,写两个简单的demo如下:

<Window x:Class="StackPanel_One.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:StackPanel_One"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0" Orientation="Horizontal">
            <Button Width="40" Height="40"></Button>
            <Button Width="40" Height="40"></Button>
        </StackPanel>
        <StackPanel Grid.Column="1" Orientation="Vertical">
            <Button Width="40" Height="40"></Button>
            <Button Width="40" Height="40"></Button>
        </StackPanel>
        <StackPanel Grid.Column="2" Orientation="Horizontal" FlowDirection ="RightToLeft">
            <Button Width="40" Height="40"></Button>
            <Button Width="40" Height="40"></Button>
        </StackPanel>
        <StackPanel Grid.Column="3" Orientation="Vertical" FlowDirection="RightToLeft">
            <Button Width="40" Height="40"></Button>
            <Button Width="40" Height="40"></Button>
        </StackPanel>
    </Grid>
</Window>

效果如下:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
StackPanelWPF 中常用的容器控件之一,它可以将其子控件按照垂直或水平的方式排列。下面是一些关于 StackPanel 的使用方法: 1. 垂直布局: ```xaml <StackPanel Orientation="Vertical"> <Button Content="Button1"/> <Button Content="Button2"/> <Button Content="Button3"/> </StackPanel> ``` 上述代码创建了一个垂直布局StackPanel,并在其中添加了三个 Button。 2. 水平布局: ```xaml <StackPanel Orientation="Horizontal"> <Button Content="Button1"/> <Button Content="Button2"/> <Button Content="Button3"/> </StackPanel> ``` 上述代码创建了一个水平布局StackPanel。 3. 嵌套布局: 可以在一个 StackPanel 中嵌套另一个 StackPanel,以实现更复杂的布局效果。例如,可以在一个垂直布局StackPanel 中再添加一个水平布局StackPanel: ```xaml <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Button Content="Button1"/> <Button Content="Button2"/> </StackPanel> <StackPanel Orientation="Horizontal"> <Button Content="Button3"/> <Button Content="Button4"/> </StackPanel> </StackPanel> ``` 上述代码创建了一个垂直布局StackPanel,在其中分别嵌套了两个水平布局StackPanel,每个水平布局中都有两个 Button。 通过使用 StackPanel,可以方便地实现控件的自动排列布局。同时,StackPanel 还提供了一些其他属性,如对齐方式和间距设置,可以根据需要进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值