wpf之StackPanel

前言

StackPanel是WPF 中的布局容器之一,它的核心功能是将其子元素按水平或者垂直方向进行排列。

1、Orientation

该属性指定StackPanel中元素的排列方式,是水平排列还是垂直排列。

1)Vertical

元素垂直方向紧凑排列。

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Vertical"    >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 2" />
    </StackPanel>
</Window>

2)Horizontal

元素水平方向紧凑排列。

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal"     >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 2" />
    </StackPanel>
</Window>

2、HorizontalAlignment

该属性指定元素水平方向上的对齐方式

1)Left

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Vertical"       HorizontalAlignment="Left"                   >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 2" />
    </StackPanel>
</Window>

2)Right

3)Center

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Vertical"       HorizontalAlignment="Center"                     >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 2" />
    </StackPanel>
</Window>

4)Stretch

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Vertical"       HorizontalAlignment="Stretch"                      >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 2" />
    </StackPanel>
</Window>

3、VerticalAlignment

该属性指定元素垂直方向上的对齐方式

1)Top

2)Bottom

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal"        VerticalAlignment ="Bottom"    >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 3" />
    </StackPanel>
</Window>

3)Center

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal"        VerticalAlignment ="Center"    >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 3" />
    </StackPanel>
</Window>

4)Stretch

csharp 复制代码
<Window x:Class="wpf之StackPanel.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:wpf之StackPanel"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal"        VerticalAlignment ="Stretch"     >
        <TextBlock  Background="Red"  Text=" 1" />
        <TextBlock  Background="Blue"  Text=" 2" />
        <TextBlock  Background="Green"  Text=" 3" />
    </StackPanel>
</Window>

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》

2、《C#与PLC通信从入门到精通 》

3、《C# Modbus通信从入门到精通》

4、《C#Socket通信从入门到精通 》

5、《C# MES通信从入门到精通》

6、《winform控件从入门到精通》

7、《C#操作MySql数据库从入门到精通》

相关推荐
好望角雾眠3 小时前
第四阶段C#通讯开发-5:TCP
网络·笔记·网络协议·tcp/ip·c#
InCerry4 小时前
.NET周刊【11月第1期 2025-11-02】
c#·.net周报·.net周刊
李趣趣7 小时前
C#中关于ContextMenuStrip批量添加Item的问题
开发语言·c#
数据的世界017 小时前
C#权威指南第9课:方法
microsoft·c#·.net
张人玉7 小时前
C# 串口通讯中 SerialPort 类的关键参数和使用方法
开发语言·c#·串口通讯
时光追逐者12 小时前
一款基于 .NET WinForm 开源、轻量且功能强大的节点编辑器,采用纯 GDI+ 绘制无任何依赖库仅仅100+Kb
c#·.net·winform
sali-tec12 小时前
C# 基于halcon的视觉工作流-章58-输出点云图
开发语言·人工智能·算法·计算机视觉·c#
白雪公主的后妈12 小时前
Auto CAD二次开发——文字样式
c#·cad二次开发·文字样式
智者知已应修善业13 小时前
【c# 想一句话把 List<List<string>>的元素合并成List<string>】2023-2-9
经验分享·笔记·算法·c#·list
FuckPatience13 小时前
C# 接口隔离的一个案例
c#