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

相关推荐
m5655bj3 小时前
通过 C# 将 RTF 文档转换为图片
开发语言·c#
wuli_滔滔4 小时前
【探索实战】深入浅出:使用Kurator Fleet实现跨云集群的统一应用分发
架构·wpf·kurator·fleet
MM_MS4 小时前
WinForm+C#小案例--->写一个记事本程序
开发语言·计算机视觉·c#·visual studio
浪客川6 小时前
高效日志分离器:一键筛选关键信息
开发语言·windows·c#
小熊熊知识库7 小时前
C# EF.core 介绍以及高性能使用
开发语言·c#
雨疏风骤12407 小时前
【FreeRTOS】任务、任务状态
开发语言·stm32·c#·rtos
松☆7 小时前
Flutter 与 OpenHarmony 深度集成:自定义 MethodChannel 插件开发全指南
flutter·wpf
️公子9 小时前
传奇游戏集成系统
游戏·c#
玩泥巴的9 小时前
强的飞起的 Roslyn 编译时代码生成,实现抽象类继承与依赖注入的自动化配置
c#·.net·代码生成·roslyn
mudtools9 小时前
强的飞起的 Roslyn 编译时代码生成,实现抽象类继承与依赖注入的自动化配置
c#·.net