首页
学习
活动
专区
圈层
工具
发布
50 篇文章
1
【愚公系列】2023年09月 WPF控件专题 XAML介绍
2
【愚公系列】2023年09月 WPF控件专题 WPF应用程序组成
3
【愚公系列】2023年09月 WPF控件专题 Window窗体属性和事件
4
【愚公系列】2023年09月 WPF控件专题 Label、TextBox、PasswordBox控件介绍
5
【愚公系列】2023年09月 WPF控件专题 Button控件详解
6
【愚公系列】2023年09月 WPF控件专题 RadioButton控件详解
7
【愚公系列】2023年09月 WPF控件专题 CheckBox控件详解
8
【愚公系列】2023年09月 WPF控件专题 Image控件详解
9
【愚公系列】2023年09月 WPF控件专题 Border控件详解
10
【愚公系列】2023年09月 WPF控件专题 ComboBox控件详解
11
【愚公系列】2023年09月 WPF控件专题 ListBox控件详解
12
【愚公系列】2023年09月 WPF控件专题 DatePicker控件详解
13
【愚公系列】2023年09月 WPF控件专题 Calendar控件详解
14
【愚公系列】2023年09月 WPF控件专题 Slider控件详解
15
【愚公系列】2023年09月 WPF控件专题 ProgressBar控件详解
16
【愚公系列】2023年10月 WPF控件专题 StackPanel控件详解
17
【愚公系列】2023年10月 WPF控件专题 WrapPanel控件详解
18
【愚公系列】2023年10月 WPF控件专题 DockPanel控件详解
19
【愚公系列】2023年10月 WPF控件专题 Canvas控件详解
20
【愚公系列】2023年10月 WPF控件专题 Grid控件详解
21
【愚公系列】2023年10月 WPF控件专题 Groupbox控件详解
22
【愚公系列】2023年10月 WPF控件专题 Expander控件详解
23
【愚公系列】2023年10月 WPF控件专题 TabControl控件详解
24
【愚公系列】2023年10月 WPF控件专题 Frame控件详解
25
【愚公系列】2023年10月 WPF控件专题 ListView控件详解
26
【愚公系列】2023年10月 WPF控件专题 DataGrid控件详解
27
【愚公系列】2023年10月 WPF控件专题 Menu控件详解
28
【愚公系列】2023年10月 WPF控件专题 ContextMenu控件详解
29
【愚公系列】2023年10月 WPF控件专题 TreeView控件详解
30
【愚公系列】2023年10月 WPF控件专题 ToolBar控件详解
31
【愚公系列】2023年10月 WPF控件专题 ToolBarTray控件详解
32
【愚公系列】2023年10月 WPF控件专题 StatusBar控件详解
33
【愚公系列】2023年11月 WPF控件专题 MediaElement控件详解
34
【愚公系列】2023年11月 WPF控件专题 RichTextBox控件详解
35
【愚公系列】2023年11月 WPF控件专题 GridView控件详解
36
【愚公系列】2023年11月 WPF控件专题 Rectangle控件详解
37
【愚公系列】2023年11月 WPF控件专题 Popup控件详解
38
【愚公系列】2023年11月 WPF控件专题 OpenFileDialog控件详解
39
【愚公系列】2023年11月 WPF控件专题 SaveFileDialog控件详解
40
【愚公系列】2023年11月 WPF控件专题 RepeatButton控件详解
41
【愚公系列】2023年11月 WPF控件专题 Ellipse控件详解
42
【愚公系列】2023年11月 WPF控件专题 Polygon控件详解
43
【愚公系列】2023年11月 WPF控件专题 Path控件详解
44
【愚公系列】2023年11月 WPF控件专题 WindowFormsHost控件详解
45
【愚公系列】2023年11月 WPF控件专题 WebBrowser控件详解
46
【愚公系列】2023年11月 WPF控件专题 Validation控件详解
47
【愚公系列】2023年11月 WPF控件专题 Page控件详解
48
【愚公系列】2023年11月 WPF控件专题 PrintDialog控件详解
49
【愚公系列】2023年11月 WPF控件专题 Track控件详解
50
【愚公系列】2023年11月 WPF控件专题 Polyline控件详解

【愚公系列】2023年10月 WPF控件专题 StackPanel控件详解

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。

🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏

🚀前言

WPF控件是Windows Presentation Foundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。

原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见的标准用户界面元素。

自定义控件则允许开发人员使用XAML和C#等编程语言来创建个性化的用户界面元素。自定义控件可以根据需求提供更多的功能和自定义化选项,以及更好的用户体验。

🚀一、StackPanel控件详解

WPF中的StackPanel控件是一种容器控件,可以用来排列其子元素,使它们垂直或水平地堆叠。StackPanel可以在垂直方向或水平方向上排列元素,具体取决于Orientation属性。StackPanel也可以通过对齐属性来定位子元素,如HorizontalAlignment和VerticalAlignment。

下面是一个使用StackPanel控件的示例:

代码语言:html
复制
<StackPanel Orientation="Vertical">
    <TextBlock Text="Hello" />
    <TextBlock Text="World" />
</StackPanel>

这个StackPanel控件包含了两个TextBlock子元素,这两个元素会在垂直方向上堆叠。如果将Orientation属性设置为Horizontal,则两个TextBlock元素会在水平方向上堆叠。

StackPanel也可以包含其他的容器控件,例如Grid和StackPanel。

代码语言:html
复制
<StackPanel Orientation="Horizontal">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" Text="One" />
        <TextBlock Grid.Row="0" Grid.Column="1" Text="Two" />
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Three" />
        <TextBlock Grid.Row="1" Grid.Column="1" Text="Four" />
    </Grid>
    <StackPanel>
        <TextBlock Text="Five" />
        <TextBlock Text="Six" />
    </StackPanel>
</StackPanel>

这个StackPanel控件包含了一个Grid和一个StackPanel。Grid控件包含了四个TextBlock子元素,这些元素被用Grid.Row和Grid.Column定位。StackPanel包含了两个TextBlock子元素,这两个元素会在垂直方向上堆叠。整个StackPanel会将Grid和StackPanel在水平方向上堆叠。

🔎1.属性介绍

StackPanel是WPF中一种常用的面板控件,它可以使子元素按照给定的方向(横向或者纵向)依次排列。以下是StackPanel控件的属性:

  1. Orientation属性:用于指定子元素的排列方向,可选项为Horizontal(横向)和Vertical(纵向)。
  2. IsItemsHost属性:指示此StackPanel是否作为ItemsPresenter的主机,这允许它成为ItemsControl(如ListBox或ComboBox)的模板部分。
  3. CanHorizontallyScroll和CanVerticallyScroll属性:用于指定是否启用水平或垂直滚动。
  4. HorizontalAlignment和VerticalAlignment属性:用于指定StackPanel在其父元素中的水平对齐和垂直对齐方式。
  5. Height和Width属性:用于指定StackPanel的高度和宽度。
  6. Margin属性:用于指定StackPanel在其父元素中的边距。

🔎2.常用场景

StackPanel控件是一种非常常见的布局控件,常用场景包括:

  1. 垂直布局:当需要将多个控件按照垂直方向排列时,可以使用StackPanel控件来实现这个布局效果。例如,一个垂直按钮菜单或者一个竖直的导航栏。
  2. 水平布局:当需要将多个控件按照水平方向排列时,也可以使用StackPanel控件。例如,水平按钮菜单或者页眉。
  3. 没有复杂的嵌套:StackPanel控件通常建议使用相对较简单的布局需求,如果需要更复杂的布局,建议使用Grid或其他更高级的布局控件。
  4. 动态添加控件:StackPanel控件可以动态添加子控件,方便动态布局。例如,根据不同条件添加不同的控件到容器中。
  5. 静态布局:StackPanel控件也可以用于静态布局,如果需要静态的展示一些信息,而不需要用户进行交互,可以使用StackPanel来简单地实现布局。

StackPanel控件可以用于很多简单和常用的布局场景,可以快速实现一些页面或者界面的简单布局需求。

🔎3.具体案例

代码语言:c#
复制
<!--排列成一行或一列  StackPanel默认排列方向:垂直:宽度 水平:高度与父窗口的高度相同-->
<!--子元素超出的部分,会被隐藏-->
<!--<StackPanel Orientation="Vertical" HorizontalAlignment="Center">
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
</StackPanel>-->
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" FlowDirection="RightToLeft">
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
    <Button>按钮1</Button>
    <Button Margin="10,0,10,0">按钮2</Button>
    <Button>按钮3</Button>
</StackPanel>

我正在参与2023腾讯技术创作特训营第二期有奖征文,瓜分万元奖池和键盘手表

下一篇
举报
领券