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>

效果图

相关推荐
FuckPatience11 小时前
WPF 具有跨线程功能的UI元素
wpf
诗仙&李白13 小时前
HEFrame.WpfUI :一个现代化的 开源 WPF UI库
ui·开源·wpf
He BianGu15 小时前
【笔记】在WPF中Binding里的详细功能介绍
笔记·wpf
He BianGu19 小时前
【笔记】在WPF中 BulletDecorator 的功能、使用方式并对比 HeaderedContentControl 与常见 Panel 布局的区别
笔记·wpf
123梦野2 天前
WPF——效果和可视化对象
wpf
He BianGu2 天前
【笔记】在WPF中Decorator是什么以及何时优先考虑 Decorator 派生类
笔记·wpf
时光追逐者2 天前
一款专门为 WPF 打造的开源 Office 风格用户界面控件库
ui·开源·c#·.net·wpf
He BianGu2 天前
【笔记】介绍 WPF XAML 中 Binding 的 StringFormat详细功能
笔记·wpf
Rotion_深3 天前
C# WPF使用线程池运行Action方法
c#·wpf·线程池
攻城狮CSU4 天前
WPF 深入系列.2.布局系统.尺寸属性
wpf