WPF布局之WrapPanel与StackPanel

article/2025/10/25 16:04:09

转载:https://www.cnblogs.com/Im-Victor/p/10565030.html

三. WrapPanel

  WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够是就会自动调整进行换行,后续排序按照从上至下或从右至左的顺序进行。

 Orientation——根据内容自动换行。当 Horizontal选项看上去类似于Windows资源管理器的缩略图视图:元素是从左向右排列的,然后自上至下自动换行。Vertical 选项看上去类似于Windows资源管理器的列表视图:元素是从上向下排列的,然后从左至右自动换行。

   ItemHeight——所有子元素都一致的高度。每个子元素填充高度的方式取决于它的VerticalAlignment属性、Height属性等。任何比ItemHeight高的元素都将被截断。

   ItemWidth——所有子元素都一致的宽度。每个子元素填充高度的方式取决于它的VerticalAlignment属性、Width属性等。任何比ItemWidth高的元素都将被截断。

 

本次的示例,效果图如下2图,图1是宽度比较小,图2就是拉长了宽度后的结果。大家可以在实际做出来之后,自行拉动窗体的宽度:

 

                图1

 

                                    图2

 

上面两图的XAML代码实现:

复制代码

<Window x:Class="WpfApp1.WindowWrap"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WindowWrap" Height="300" Width="400"><Grid><WrapPanel  Orientation="Horizontal"><TextBlock Name="textBlock_CityID" Text="CityID:" /><TextBox Name="textBox_CityID" MinWidth="100" /><TextBlock Name="textBlock_CityName" Text="CityName:" /><TextBox Name="textBox_CityName" MinWidth="100" /><TextBlock Name="textBlock_ZipCode" Text="ZipCode:" /><TextBox Name="textBox_ZipCode" MinWidth="100"  /><TextBlock Name="textBlock_ProvinceID" Text="ProvinceID:" /><TextBox Name="textBox_ProvinceID" MinWidth="100"   /><TextBlock Name="textBlock_DateCreated" Text="DateCreated:"  /><TextBox Name="textBox_DateCreated" MinWidth="100"   /><TextBlock Name="textBlock_DateUpdated" Text="DateUpdated:" /><TextBox Name="textBox_DateUpdated" MinWidth="100" /></WrapPanel></Grid></Window>

复制代码

C#代码实现上图示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

using System;

 

using System.Collections.Generic;

 

using System.Linq;

 

using System.Text;

 

using System.Threading.Tasks;

 

using System.Windows;

 

using System.Windows.Controls;

 

using System.Windows.Data;

 

using System.Windows.Documents;

 

using System.Windows.Input;

 

using System.Windows.Media;

 

using System.Windows.Media.Imaging;

 

using System.Windows.Shapes;

 

  

 

namespace WpfApp1

 

{

 

    /// <summary>

 

    /// WindowWrap.xaml 的交互逻辑

 

    /// </summary>

 

    public partial class WindowWrap : Window

 

    {

 

        public WindowWrap()

 

        {

 

            InitializeComponent();

 

        }

 

  

 

        private void btnAddByCode_Click(object sender, RoutedEventArgs e)

 

        {

 

            WrapPanel wp = new WrapPanel();

 

            //把wp添加为窗体的子控件

 

            this.Content = wp;

 

            wp.Margin = new Thickness(0, 0, 0, 0);

 

            wp.Background = new SolidColorBrush(Colors.White);

 

            //遍历增加TextBlock

 

            TextBlock block;

 

            for (int i = 0; i <= 10; i++)

 

            {

 

                block = new TextBlock();

 

                block.Text = "后台代码添加控件:" + i.ToString();

 

                block.Margin = new Thickness(10, 10, 10, 10);

 

                block.Width = 160;

 

                block.Height = 30;

 

                wp.Children.Add(block);

 

            }      

 

  

 

        }

 

    }

 

}

  

四. StackPanel

StackPanel就是将控件按照行或列来顺序排列,但不会换行。通过设置面板的Orientation属性设置了两种排列方式:横排(Horizontal默认的)和竖排(Vertical)。纵向的StackPanel默 认每个元素宽度与面板一样宽,反之横向亦然。如果包含的元素超过了面板空间,它只会截断多出的内容。 元素的Margin属性用于使元素之间产生一定得间隔,当元素空间大于其内容的空间时,剩余空间将由HorizontalAlignment和 VerticalAlignment属性来决定如何分配。

本示例要实现的效果如下2图,图1是横排,图2是竖排。

 

                                    图1

 

                           图2

上两图的XAML代码实现:

复制代码

<Window x:Class="WpfApp1.WindowStack"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WindowStack" Height="400" Width="500"><Grid><StackPanel Name="stackPanel" Margin="0,0,0,0" Background="White" Orientation="Vertical"><Button Content="第一个"/><Button Content="第二个"/><Button Content="第三个"/><Button Content="第四个"/><Button Content="第五个,改变排列方式" Click="Button_Click"/><Button Content="后台代码实现" Click="Button_Click_1"/></StackPanel></Grid></Window>

复制代码

 

上图示例的C#代码实现:

复制代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;namespace WpfApp1{/// <summary>/// WindowStack.xaml 的交互逻辑/// </summary>public partial class WindowStack : Window{public WindowStack(){InitializeComponent();}private void Button_Click(object sender, RoutedEventArgs e){stackPanel.Orientation=Orientation.Horizontal;}private void StackPanels(){StackPanel sp = new StackPanel();//把sp添加为窗体的子控件this.Content = sp;sp.Margin = new Thickness(0, 0, 0, 0);sp.Background = new SolidColorBrush(Colors.White);sp.Orientation = Orientation.Vertical;//Button1Button b1 = new Button();b1.Content = "后台代码,第一个";sp.Children.Add(b1);//Button2Button b2 = new Button();b2.Content = "后台代码,第二个";sp.Children.Add(b2);//Button3Button b3 = new Button();b3.Content = "后台代码,第三个";sp.Children.Add(b3);}private void Button_Click_1(object sender, RoutedEventArgs e){StackPanels();}}}

复制代码

注: 当把StackPanel的FlowDirection属性设置为RightToLeft,Orientation属性设置为Horizontal,StackPanel将从右向左排列元素


http://chatgpt.dhexx.cn/article/iGWnh3r0.shtml

相关文章

WrapPanel在不同页面渲染使用

WrapPanel渲染的使用 WrapPanel在UserControl用户控件的使用 页面控件的是渲染出来&#xff0c;在UserControl用户控件的使用无法直接使用&#xff0c;使用会出现空&#xff0c;没法渲染获取到WrapPanel控件。 没法渲染获取效果图&#xff1a; 想成功获取渲染获取到WrapPane…

WPF 入门教程WrapPanel介绍

WrapPanel将定位每个子控件的旁边&#xff0c;另外&#xff0c;水平方向&#xff08;默认值&#xff09;或垂直&#xff0c;直到没有更多的空间&#xff0c;在那里将换到下一行&#xff0c;然后继续。当您想要一个垂直或水平列表控件在没有更多空间时自动换行时使用它。 当 Wr…

WPF WrapPanel、UniformGrid、DockPanel介绍

WPF WrapPanel、UniformGrid、DockPanel介绍 WrapPanel WrapPanel , 具有在有限的容器范围内, 可以自动换行, 或者换列处理。具体则取决于WrapPanel的排列方式 (Orientation)。 Orientation"Horizontal"时各控件从左至右罗列&#xff0c;当面板长度不够时&#xff…

WPF控件之WrapPanel的用法

简单来说&#xff0c;WrapPanel就是一个可以行填充&#xff0c;自动换行。或者列填充&#xff0c;自动换列的一个布局容器。 如下所示&#xff0c;它会自动将10个button分成2行 <WrapPanel><Button Content"button1" /><Button Content"button…

WrapPanel:自动折行面板(环绕面板)基础简述

WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够时就会自动调整进行换行或换列&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——根据内容自动换行。当Orientation属性的值设置为 Horizontal&#xff1a;元素是…

WPF 控件专题 WrapPanel 控件详解

1、WrapPanel 介绍 官方释义&#xff1a;WrapPanel&#xff08;环绕面板&#xff09;&#xff1a;按从左到右的顺序位置定位子元素&#xff0c;在包含框的边缘处将内容切换到下一行。 后续排序按照从上至下或从右至左的顺序进行&#xff0c;具体取决于 Orientation 属性的值。 …

UI Automation编程辅助工具Inspect的下载和使用

UIAutomation微软提供的UI自动化库&#xff0c;主要用AutomationElement类来表示UI 自动化目录树中的一个UI自动化元素&#xff0c;.NET Windows的窗体应用程序和WPF应用程序。 Inspect是一款类似于SPY的界面捕捉工具。外观如下&#xff1a; 下载地址&#xff1a; Inspect.zip …

docker inspect --format 详解

docker inspect --format 文章目录 docker inspect --format1. 什么是模板2. Go模板语法3. 变量3.1 系统变量 {{.}}3.2 自定义变量3.3 遍历&#xff08;循环&#xff09;&#xff1a;range 4. index5. 判断5.1 not5.2 or5.3 判断条件5.4 判断示例 6. 打印信息9. 管道10. 内置函…

android studio inspect code,Android Studio使用inspect code删除 unused import

项目经常需要从别的项目复制一些文件过来&#xff0c;但是经常出现大片的导包unused import&#xff0c;但是我用自动导包并没有效果&#xff0c;必须把这些错误的导包手动删除才可以导入正确的包&#xff0c;虽然文件不多&#xff0c;手动删除是可以完成的&#xff0c;但是作为…

Halcon Variable Inspect 安装失败

这里写自定义目录标题 Halcon Variable Inspect 安装失败 Halcon Variable Inspect 安装失败 在 Visual Studio 2022 环境下安装 HALCON2105ProgressVariableInspect.vsix 失败。 点击 “View Install Log” 查看日志。 主要问题在运行vsix文件时&#xff0c;它会监控然后运行…

docker inspect 查看部分数据

1. 问题描述 使用docker inspect命令可查看 docker 镜像/容器/数据卷 等的具体配置docker inspect命令格式docker inspect [参数] 容器名/容器ID/镜像名/镜像ID/数据卷名...如果不加参数&#xff0c;则输出很多&#xff0c;难以精确查看真正关心的内容 如果使用grep 表达式&am…

【网络教程】如何安装inspect.exe

文章目录 安装 安装 Inspect&#xff08;Inspect.exe&#xff09;是一个基于Windows的工具&#xff0c;允许您选择任何UI元素并查看元素的可访问性数据。您可以查看Microsoft UI自动化属性和控制模式&#xff0c;以及Microsoft活动辅助功能属性。Inspect还允许您测试UI自动化树…

inspect.exe安装使用

官网下载 https://developer.microsoft.com/zh-cn/windows/downloads/windows-sdk/ 官网教程 https://learn.microsoft.com/zh-cn/windows/win32/winauto/inspect-objects 要求 系统要求 Windows SDK 具有以下最低系统要求&#xff1a; 支持的操作系统 Windows 10版本 150…

python中inspect模块用法详解

获取函数签名对象。函数签名包含了一个函数的信息&#xff0c;包括函数名、它的参数类型、它所在的类和名称空间及其他信息&#xff09;。 inspect模块主要提供了四种用处&#xff1a; 对是否是模块、框架、函数进行类型检查获取源码获取类或者函数的参数信息解析堆栈 1、对…

docker inspect

docker inspect 命令用于获取有关 Docker 容器、镜像、网络等的详细信息。它提供了关于指定对象的元数据和配置的完整视图&#xff0c;包括运行状态、网络设置、卷挂载、环境变量等。 以下是一些常见的字段和属性&#xff0c;可以根据需要选择其中一些或全部列出&#xff1a; …

UI自动化辅助工具Inspect

了解更多访问&#xff1a;windows应用程序GUI自动化控制 Inspect&#xff08;Inspect.exe&#xff09;是一个基于Windows的工具&#xff0c;允许您选择任何UI元素并查看元素的可访问性数据。您可以查看Microsoft UI自动化属性和控制模式&#xff0c;以及Microsoft活动辅助功能…

mysql数据库基础管理--数据库权限表

mysql数据库通过权限表实现对mysql数据库的访问&#xff0c;权限表存放在mysql数据库中&#xff0c;有脚本 mysql_install_db初始化。 存储账户权限表主要 USER,DB,HOST,TABLES_PRIV,COLUMNS_PRIV和PROCS_PRIV. mysql.USER 记录允许连接到数据库的用户信息&#xff0c;里面…

1、user权限表详解

MySQL 在安装时会自动创建一个名为 mysql 的数据库&#xff0c;mysql 数据库中存储的都是用户权限表。用户登录以后&#xff0c;MySQL 会根据这些权限表的内容为每个用户赋予相应的权限。 user 表是 MySQL 中最重要的一个权限表&#xff0c;用来记录允许连接到服务器的账号信息…

MySQL中用户权限、库权限、表权限的控制

MySQL中用户权限、库权限、表权限的控制 在开发中&#xff0c;除了可以在脚本代码中控制开发者对MySQL数据库数据表的权限&#xff0c;还可以在mysql层面进行控制。 对mysql的权限的控制可以大致分为三个层面&#xff1a;①连接上允不允许&#xff08;是否能登录到mysql服务器…

数据库表设计1:用户权限管理

前言&#xff1a;权限管理是所有后台系统的都会涉及的一个重要组成部分&#xff0c;主要目的是对不同用户访问资源进行权限的控制&#xff0c;避免因权限控制缺失或操作不当引发的风险问题&#xff0c;如操作错误&#xff0c;隐私数据泄露等问题。 1、RBAC简介 RBAC&#xff0…