Wpf布局之StackPanel!

文章目录


前言

Wpf布局之StackPanel!


一、引言

StackPanel面板在水平或垂直的堆栈中放置元素。这个布局容器通常用于更大、更复杂窗口中的一些区域。

二、使用步骤

StackPanel默认是垂直堆叠

c 复制代码
<Grid>
    <StackPanel>
        <Button Height="100" Width="100" Content="按钮1"/>
        <Button Height="100" Width="100" Content="按钮2"/>
        <Button Height="100" Width="100" Content="按钮3"/>
    </StackPanel>
</Grid>

效果图

可以将Orientation属性设置为Horizontal,就可以变成水平堆叠

c 复制代码
<Grid>
    <StackPanel Orientation="Horizontal">
        <Button Height="100" Width="100" Content="按钮1"/>
        <Button Height="100" Width="100" Content="按钮2"/>
        <Button Height="100" Width="100" Content="按钮3"/>
    </StackPanel>
</Grid>

效果如图

上面的按钮都是紧挨在一起的,如果想要让按钮之间留点缝隙,可以使用Margin属性,当Margin属性的值设置为一个代表着为所有的边都设置相同的宽度。

c 复制代码
<Grid>
    <StackPanel Orientation="Horizontal">
        <Button Margin="10" Height="100" Width="100" Content="按钮1"/>
        <Button Margin="10" Height="100" Width="100" Content="按钮2"/>
        <Button Margin="10" Height="100" Width="100" Content="按钮3"/>
    </StackPanel>
</Grid>

效果图

也可以为每条边单独设置一个值,值设置的顺序是从左开始顺时针,即左、上、右、下。

c 复制代码
<Grid>
    <StackPanel Orientation="Horizontal">
        <Button Margin="5,10,15,20" Height="100" Width="100" Content="按钮1"/>
        <Button Height="100" Width="100" Content="按钮2"/>
        <Button Height="100" Width="100" Content="按钮3"/>
    </StackPanel>
</Grid>

效果图

也可以只为左右设置相同的值或者上下设置相同的值。下面代码中Margin="5,15",代表着左右值边距都设置为5,上下边距都设置为15。

c 复制代码
<Grid>
    <StackPanel Orientation="Horizontal">
        <Button Margin="5,15" Height="100" Width="100" Content="按钮1"/>
        <Button Height="100" Width="100" Content="按钮2"/>
        <Button Height="100" Width="100" Content="按钮3"/>
    </StackPanel>
</Grid>

效果图

相关推荐
hqwest3 小时前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple3 小时前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题1 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘2 天前
开发WPF项目时遇到的问题总结
wpf
hqwest3 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars3 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest3 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel
hqwest3 天前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
wuty0074 天前
WPF 实现支持动态调整高度的文本显示控件
wpf·scrollviewer·extentheight·自动高度控件·动态调整高度
范纹杉想快点毕业7 天前
C 语言主控开发与显控开发能力体系及技术栈详解,STM32、QT、嵌入式、边缘系统显示
stm32·单片机·tcp/ip·microsoft·fpga开发·51单片机·wpf