转载: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将从右向左排列元素

WPF布局之WrapPanel与StackPanel相关推荐

  1. .NET WPF教程(7)——布局介绍WrapPanel与StackPanel(②)

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

  2. WPF入门教程系列七——布局之WrapPanel与StackPanel(二)

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

  3. WPF布局控件之StackPanel

    StackPanel Stack,英文意思是堆栈,StackPanel,意思是堆栈式布局,相当于把控件给堆起来.如果不设置StackPanel中控件的宽高,那么其中控件的宽高是默认和StackPane ...

  4. (转)WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel

    回顾 上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当 然这些都是本人在实际项目中的使用经验,可能还存在错误之处,还请大家指出. 本 ...

  5. ( 转)WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel

    回顾 上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当 然这些都是本人在实际项目中的使用经验,可能还存在错误之处,还请大家指出. 本 ...

  6. WPF 布局 - Grid, StackPanel, DockPanel, WrapPanel

    本文大纲 1.Grid 2.StackPanel 3.DockPanel 4.WrapPanel Grid 1.Row和Column 我们下面来介绍Grid的行的用法,及我们在UI设计过程中需要注意的 ...

  7. WPF中WrapPanel、StackPanel等添加滚动条ScrollViewer

    wpf中,在控件中直接设置ScrollViewer.HorizontalScrollBarVisibility和ScrollViewer.VerticalScrollBarVisibility属性,并 ...

  8. WPF编程基础入门 ——— 第三章 布局(五)布局面板WrapPanel

    WPF布局--布局面板WrapPanel WPF--WrapPanel布局控件 WrapPanel实例--十个按钮 WPF--WrapPanel布局控件 WrapPanel(自动折行面板),允许任意多 ...

  9. WPF布局控件之WrapPanel

    WrapPanel WrapPanel,英文意思是折叠容器,那到底是怎么个折叠法呢?如下: <Window x:Class="LearnLayout.WrapPanelWin" ...

  10. WPF教程三:布局之WrapPanel面板(转 )

    WPF教程三:布局之WrapPanel面板 WrapPanel:环绕面板 WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够时就会自动调整进行换行,后续排序按照从上至下 ...

最新文章

  1. C语言实现bmp图像锐化
  2. ubuntu18.04下双机驱动调试
  3. component映射
  4. XTU 1243 2016
  5. Python极简入门:数据类型、条件语句、循环语句、异常处理
  6. 还在犹豫是否迁移.NET5?这几个项目已经上线了!
  7. C# 8 新特性 - 异步流 Asynchronous Streams
  8. python beautifulreport_Python unittest 之 BeautifulReport可视化报告
  9. 培训课程第三期签到和意见发表
  10. 001机器学习深度学习简介
  11. 基于Verilog实现呼吸灯
  12. easyui下拉选项多怎么解决_作物根部病害多原因在哪?解决病害生根措施怎么做?...
  13. 『原创·翻译』如何阅读论文
  14. 还不知道如何使用 IDEA ?教你三招快速掌握 IDEA
  15. tumblr_从iPhone或iPod Touch更新Tumblr博客
  16. exchange2016卸载报错安装程序无法卸载,因为mscorsvw(9476)具有打开的文件
  17. PHP:回退(Backed)枚举
  18. 用浏览器的油猴子脚本调用IDM下载百度云盘中的资料
  19. 解析在线教育培训APP开发
  20. jude(java建模软件)_JUDE(JAVA建模软件)下载

热门文章

  1. GB/T2659-2000,ISO 3166-1:1997,ISO 3166-1:2006国家和地区代码列表(已整理)
  2. 爬虫python代码网易云_Python爬虫之网易云音乐下载
  3. linux limbo镜像文件下载,limbo linux镜像下载
  4. html实现简易影院购票,打造属于自己的私人影院,竟然这么简单!
  5. 脑筋急转弯合集,主治心情不好!开心一笑
  6. 2020年最值得关注的28款区块链游戏
  7. optistruct中的DRESP2响应设置
  8. 在Win7系统中如何安装PDF虚拟打印机
  9. word太大解决方法------图片压缩和visio图片批量压缩
  10. tiledmap 图块属性_cocos2dx[3.4](25)——瓦片地图TiledMap