WPF布局之WrapPanel与StackPanel

WPF布局之WrapPanel与StackPanel转载:https://www.cnblogs.com/Im-Victor/p/10565030.html三.WrapPanelWrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列,当长度或高度不够是就会自动调整进行换行,后续排序按照从上至下或从右至左的顺序进行。Orientation——根据内容自动换行。当Horizontal选项看上去类似于Windows资源管理器的缩略图视图:元素是从左向右排列的,然后自上至下自动换行。Vertical选项看上去类似于Windows资源..

大家好,又见面了,我是你们的朋友全栈君。

转载: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就是拉长了宽度后的结果。大家可以在实际做出来之后,自行拉动窗体的宽度:

 WPF布局之WrapPanel与StackPanel

                图1

 

WPF布局之WrapPanel与StackPanel

                                    图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>

复制代码

WPF布局之WrapPanel与StackPanel

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是竖排。

 WPF布局之WrapPanel与StackPanel

                                    图1

 WPF布局之WrapPanel与StackPanel

                           图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>

复制代码

 

WPF布局之WrapPanel与StackPanel

上图示例的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;

            //Button1

            Button b1 = new Button();

            b1.Content = "后台代码,第一个";

            sp.Children.Add(b1);

 

            //Button2

            Button b2 = new Button();

            b2.Content = "后台代码,第二个";

            sp.Children.Add(b2);

 

            //Button3

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163321.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux ip 配置及配置保存

    linux ip 配置及配置保存1配置IP#改对应网卡配置IP ifconfigeth010.120.16.82/24#配置默认路由routeadddefaultgw10.120..16.1#利用route命令配置路由如将192.168.20.X的ip路由到192.168.20.254网关routeadd-net192.168.20.0/24gw192.168.20.25

    2022年5月20日
    193
  • 红楼品鉴「建议收藏」

    红楼品鉴「建议收藏」红楼梦曹雪芹(1715-1763)案头文学,很多心理活动,心理描写很成功,特别是黛玉,因此在电视剧中黛玉的角色很难演,但是在戏曲中通过唱词比较好表达。本人强推裴效维评注的红楼梦版本,全套三本,评注很详细,有很多诗词的解释。(一)人物关系贾代善娶了金陵史家的小姐(贾母)宁国府人丁稀少,贾敬很早就到寺庙里炼丹求药,将他的爵位世袭给了贾珍。水字辈、代字辈、文字辈、…

    2022年6月8日
    29
  • JavaWeb专栏之(三):Eclipse创建JavaWeb项目「建议收藏」

    JavaWeb专栏之(三):Eclipse创建JavaWeb项目「建议收藏」JavaWeb专栏之(三):Eclipse创建JavaWeb项目前言:关注:《遇见小Du说》微信公众号,分享更多Java知识,不负每一次相遇。更多内容请访问:www.dushunchang.top在上一篇文章中,小Du猿带大家使用Idea创建JavaWeb项目,相比之下Idea作为当前非常主流的开发IDE,深受Java后端程序员使用。市面上约75%开发者使用Idea,一代开发神器Eclipse就此没落。小Du猿第一次使用的开发IDE就是Eclipse,也算是我的启蒙神器。今天就带了使

    2022年6月18日
    21
  • siamfc++代码_c语言代码怎么理解

    siamfc++代码_c语言代码怎么理解文章目录前言一、论文翻译二、论文代码1.backbone网络前言记录自己阅读复现SiamFC的全过程,包括论文翻译,代码理解等一、论文翻译论文原文:链接:https://pan.baidu.com/s/1wvXra0Ji6L9IMVZikaUs9Q提取码:s7t3本文是Siam系列跟踪论文的开篇之作,兼容了速度与精度,引起跟踪社区极大的关注。论文中对一些细节描述分非常充分,适合精读本文。二、论文代码代码参考;https://github.com/HonglinChu/SiamTra.

    2022年9月30日
    0
  • f1-score是什么_python概念题

    f1-score是什么_python概念题一、F1score概念?F1score是分类问题的一个衡量指标,一些多分类问题的机器学习竞赛,常把F1score作为最终评测的方法。它是精确率和召回率的调和平均数,取值0-1之间。F1score认为召回率和精确率同样重要,而F2认为召回率的重要程度是精确率的2倍,F0.5则认为召回率的重要程度是精确率的一半。要明确几个概念TP(TruePositive):被判定为正样本,实际为正样本 TN(TrueNegative):被判定为负样本,实际为负样本 FP(FalseP

    2022年10月14日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号