当前位置: 首页 > news >正文

wpf之StackPanel

前言

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

1、Orientation

该属性指定StackPanel中元素的排列方式,是水平排列还是垂直排列。
1)Vertical
元素垂直方向紧凑排列。

<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
元素水平方向紧凑排列。

<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

<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

<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

<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

<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

<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

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

http://www.dtcms.com/a/361656.html

相关文章:

  • 一、Git与Gitee常见问题解答
  • 2025年数字化转型关键证书分析与选择指南
  • Spark和Spring整合处理离线数据
  • 在idea当中git的基础使用
  • Ansible变量与机密管理总结
  • 人工智能学习:什么是NLP自然语言处理
  • 【自记录】Ubuntu20.04下Python自编译
  • 全栈智算系列直播 | 智算中心对网络的需求与应对策略(上)
  • 基于FPGA的多协议视频传输IP方案
  • 【系统架构师设计(8)】需求分析之 SysML系统建模语言:从软件工程到系统工程的跨越
  • 硬件开发_基于Zigee组网的果园养殖监控系统
  • 简单高效的“色差斑块”匀色、水体修补、地物修复技巧
  • 51.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--登录注册扩展
  • 开源项目_CN版金融分析工具TradingAgents
  • Linux权限详解:从基础到实践
  • Selenium 4 文件上传和下载操作指南
  • kubernetes应用的包管理Helm工具
  • MySql blob转string
  • 15693协议ICODE SLI 系列标签应用场景说明及读、写、密钥认证操作Qt c++源码,支持统信、麒麟等国产Linux系统
  • 【Pycharm】Pychram软件工具栏Git和VCS切换
  • 【数据可视化-102】苏州大学招生计划全解析:数据可视化的五大维度
  • 从零开始实现Shell | Linux进程调度实战
  • AI时代SEO关键词实战解析
  • Scala协变、逆变、上界/下界、隐式参数、隐式转换
  • daily notes[7]
  • Windows系统下如何配置和使用jfrog.exe
  • Ansible变量的定义与使用
  • docker 网络配置
  • MJ Prompt Tool-好用的Midjourney提示词工具
  • uniApp 混合开发全指南:原生与跨端的协同方案