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数据库从入门到精通》

相关推荐
yantuguiguziPGJ6 小时前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
Aevget6 小时前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(二)
.net·wpf·界面控件·devexpress·ui开发·数据网格
从孑开始9 小时前
ManySpeech.MoonshineAsr 使用指南
人工智能·ai·c#·.net·私有化部署·语音识别·onnx·asr·moonshine
YuanlongWang9 小时前
C# 中,依赖注入(DI)的实现方式
c#
SmartSoftHelp开发辅助优化10 小时前
C# WinForm 编程高手:程序,进程,线程。程序,窗体,UI,后台。是如何协调工作的?深度解析>SmartSoftHelp魔法精灵工作室
microsoft·ui·c#
大美B端工场-B端系统美颜师11 小时前
工控软件开发选择难?Electron、Qt、WPF 对比
qt·electron·wpf
future_studio12 小时前
聊聊 Unity(小白专享、C# 小程序 之 加密存储)
开发语言·小程序·c#
c#上位机13 小时前
MefBootstrapper在Prism引导程序中的使用
c#·wpf·prism
玩泥巴的16 小时前
.NET驾驭Word之力:基于规则自动生成及排版Word文档
c#·word·.net·com互操作
SunnyDays101116 小时前
C# 实现高保真 Excel 转 PDF(无需 Office 环境)
经验分享·c#·excel转pdf