对wpf 的入门记录总结----面板控件Canvas、WrapPanel、StackPanel、DockPanel

article/2025/10/25 7:13:05

面板是WPF裡其中一個很重要的控件。
面板扮演著裝載其他控件的容器的角色,同時也控制著頁面和視窗的佈局。

由於一個視窗只允許一個子控件,因此面板經常會被使用於分隔空間,這樣每個空間就會有一個控件或者面板。

wpf支持6种面板:

1. Canvas

这是一种简单的面板,与WinForms应用处理方式类似。该面板可以设置每个子控件的坐标,容许完全的布局控制。但是该面板不够弹性,因为你必须手动移动子控件以保证他们按照你需要的位置和方式排列。推荐在你想要完全自己布置子控件时选用。

canvas不会真正关心是否有足够的空间容纳所有控件或者是否有一个位于另一个控件之上。这种行为使得画布对于几乎任何类型的对话框设计来说都是一个糟糕的选择。

但正如其名称所暗示的那样,画布至少对于一件事情非常有用:绘画。WPF有一堆可以放在画布中的控件,使得画布可以做出漂亮的插图。

2. WrapPanel

将每个子控件按照水平(默认方式)或者竖直的方式满布一行或一列,让后再布置下一行或者下一列。当你需要水平或者竖直排列子控件且能自动滚动进入下一行(列)时采用他。

<Window x:Class="WpfTutorialSamples.Panels.WrapPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WrapPanel" Height="300" Width="300"><WrapPanel><Button>Test button 1</Button><Button>Test button 2</Button><Button>Test button 3</Button><Button Height="40">Test button 4</Button><Button>Test button 5</Button><Button>Test button 6</Button></WrapPanel>
</Window>

在这里插入图片描述
将“方向”设置为“垂直”时,所有这些行为也都是如此

<Window x:Class="WpfTutorialSamples.Panels.WrapPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="WrapPanel" Height="120" Width="300"><WrapPanel Orientation="Vertical"><Button>Test button 1</Button><Button>Test button 2</Button><Button>Test button 3</Button><Button Width="140">Test button 4</Button><Button>Test button 5</Button><Button>Test button 6</Button></WrapPanel>
</Window>

在这里插入图片描述
如果第四个按钮自定义了宽度和高度

<Button Width="140" Height="44">Test button 4</Button>

在这里插入图片描述

3. StackPanel

StackPanel 的行为与 WrapPanel 很相似,但与 WrapPanel 會自动滚动過長的子控件行為不同,它會盡量延長自己。

与WrapPanel类似,它的方向可以是水平或垂直,默认是垂直,但每个项目拉伸占满全宽或全高,而不是基于最大的项目调整宽度或高度。
当你想要一连串控制项尽可能填满空间,就使用StackPanel 。

<Window x:Class="WpfTutorialSamples.Panels.StackPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="StackPanel" Height="160" Width="300"><StackPanel><Button>Button 1</Button><Button>Button 2</Button><Button>Button 3</Button><Button>Button 4</Button><Button>Button 5</Button><Button>Button 6</Button></StackPanel>
</Window>

在这里插入图片描述
容易地針對排列方向性質做修改:

<StackPanel Orientation="Horizontal">

在这里插入图片描述
因為子控件的水平對齊或者垂直對齊的屬性設定為延展(Stretch),但是如果你需要,你可以很容易的修改這設定。

<Window x:Class="WpfTutorialSamples.Panels.StackPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="StackPanel" Height="160" Width="300"><StackPanel Orientation="Horizontal"><Button VerticalAlignment="Top">Button 1</Button><Button VerticalAlignment="Center">Button 2</Button><Button VerticalAlignment="Bottom">Button 3</Button><Button VerticalAlignment="Bottom">Button 4</Button><Button VerticalAlignment="Center">Button 5</Button><Button VerticalAlignment="Top">Button 6</Button></StackPanel>
</Window>

通过子控件使用垂直或者水平對齊,彻底改变了StackPanel外观:
在这里插入图片描述

<Window x:Class="WpfTutorialSamples.Panels.StackPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="StackPanel" Height="160" Width="300"><StackPanel Orientation="Vertical"><Button HorizontalAlignment="Left">Button 1</Button><Button HorizontalAlignment="Center">Button 2</Button><Button HorizontalAlignment="Right">Button 3</Button><Button HorizontalAlignment="Right">Button 4</Button><Button HorizontalAlignment="Center">Button 5</Button><Button HorizontalAlignment="Left">Button 6</Button></StackPanel>
</Window>

在这里插入图片描述

4. DockPanel

DockPanel允许您将子控件停靠在顶部、底部、左侧或右侧。默认情况下,如果没有给定特定的dock位置,最后一个控件将填充剩余的空间。

您可以使用Grid面板实现相同的操作,但是对于更简单的情况,DokPanel将更易于使用。
每当需要将一个或多个控件停靠到一个侧边时,使用DockPanel,比如将窗口划分为特定区域。

<Window x:Class="WpfTutorialSamples.Panels.DockPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="DockPanel" Height="250" Width="250"><DockPanel><Button DockPanel.Dock="Left">Left</Button><Button DockPanel.Dock="Top">Top</Button><Button DockPanel.Dock="Right">Right</Button><Button DockPanel.Dock="Bottom">Bottom</Button><Button>Center</Button></DockPanel>
</Window>

第一个控件将停靠在左边,最后一个控件占用剩余的空间。
在这里插入图片描述
我们还可以通过为子控件指定宽度/高度来调整空间

<Window x:Class="WpfTutorialSamples.Panels.DockPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="DockPanel" Height="250" Width="250"><DockPanel><Button DockPanel.Dock="Top" Height="50">Top</Button><Button DockPanel.Dock="Bottom" Height="50">Bottom</Button><Button DockPanel.Dock="Left" Width="50">Left</Button><Button DockPanel.Dock="Right" Width="50">Right</Button>	<Button>Center</Button></DockPanel>
</Window>

在这里插入图片描述
如前所述,默认行为是,DockPanel的最后一个子节点占用其余空间,但是可以使用LastChildFill禁用此操作

<Window x:Class="WpfTutorialSamples.Panels.DockPanel"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="DockPanel" Height="300" Width="300"><DockPanel LastChildFill="False"><Button DockPanel.Dock="Top" Height="50">Top</Button><Button DockPanel.Dock="Bottom" Height="50">Bottom</Button><Button DockPanel.Dock="Left" Width="50">Left</Button><Button DockPanel.Dock="Left" Width="50">Left</Button><Button DockPanel.Dock="Right" Width="50">Right</Button><Button DockPanel.Dock="Right" Width="50">Right</Button></DockPanel>
</Window>

在这里插入图片描述


http://chatgpt.dhexx.cn/article/1y18upyb.shtml

相关文章

listbox 表头自动换行_WPF让ListView或ListBox中的WrapPanel 自动换行

原文:WPF让ListView或ListBox中的WrapPanel 自动换行 在ListView或者ListBox中使用WrapPanel想让ItemTemplate中的内容像下图这样先横向平铺然后再纵向换行&#xff0c;默认设置是不能实现的。 图1. 横向平铺再纵向换行的效果示例 请注意最关键的在上面代码中第一行的“ScrollV…

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

WPF教程三&#xff1a;布局之WrapPanel面板 WrapPanel&#xff1a;环绕面板 WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够时就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——根据内容自…

WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel

WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel 回顾 上一篇&#xff0c;我们介绍了基本控件及控件的重要属性和用法&#xff0c;我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景&#xff0c;当 然这些都是本人在实际项目中的使用经验&#xff0c;可能…

2021-08-16 WPF控件专题 WrapPanel 控件详解

1.WrapPanel 控件介绍 流面板 子元素按顺序排列&#xff0c;如果按水平方向:从左到右&#xff0c;超出部分&#xff0c;自动换行到下一行 垂直 从上到下&#xff0c; 下一列 排列方向&#xff1a;Orientation ItemWidth ItemHeight 调整面板的尺寸时&#xff0c;内部子元素的…

.NET WPF(3)布局之WrapPanel面板

一.WPF&#xff08;3&#xff09;布局之WrapPane面板 1. WrapPanel&#xff1a;环绕面板 WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够时就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orie…

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

WPF布局——布局面板WrapPanel WPF——WrapPanel布局控件WrapPanel实例——十个按钮 WPF——WrapPanel布局控件 WrapPanel(自动折行面板)&#xff0c;允许任意多的子元素按照声明的先后顺序&#xff0c;从左往右摆放&#xff0c;摆满一行后&#xff0c;自动折行。折行面板的经…

WPF布局控件之WrapPanel

WrapPanel WrapPanel&#xff0c;英文意思是折叠容器&#xff0c;那到底是怎么个折叠法呢&#xff1f;如下&#xff1a; <Window x:Class"LearnLayout.WrapPanelWin"xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"ht…

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

三. WrapPanel WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够是就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——根据内容自动换行。当 Horizontal选项看上去类似于Windows资源管理器…

WPF 用代码实现WrapPanel右侧自动对齐(解决多余空白问题)

未处理前效果&#xff1a; 处理后效果&#xff1a; <Border Background"{StaticResource BorderBg}" BorderThickness"2" BorderBrush"{StaticResource BorderBrush}" CornerRadius"5" Padding"5" x:Name"SvK…

C# Grid StackPanel DockPanel WrapPanel

WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel 回顾 上一篇&#xff0c;我们介绍了基本控件及控件的重要属性和用法&#xff0c;我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景&#xff0c;当 然这些都是本人在实际项目中的使用经验&#xff0c;可能…

WPF基础五:UI①布局元素WrapPanel

目录 WrapPanel WrapPanel类 XAML范例&#xff1a; C#代码 WrapPanel 按从左到右的顺序位置定位子元素&#xff0c;在包含框的边缘处将内容切换到下一行。 后续排序按照从上至下或从右至左的顺序进行&#xff0c;具体取决于 Orientation 属性的值。 WrapPanel包含UIElemen…

一个优化奇怪的 WrapPanel

一个优化奇怪的 WrapPanel 本文经原作者授权以原创方式二次分享&#xff0c;欢迎转载、分享。 一个优化奇怪的 WrapPanel 作者&#xff1a;陈-林-赵-魏 原文链接[1]&#xff1a;https://www.cnblogs.com/wandia/p/17092221.html FixToRB 附加属性&#xff0c;固定到【右边(水平…

WPF布局之WrapPanel与StackPanel

转载&#xff1a;https://www.cnblogs.com/Im-Victor/p/10565030.html 三. WrapPanel WrapPanel布局面板将各个控件从左至右按照行或列的顺序罗列&#xff0c;当长度或高度不够是就会自动调整进行换行&#xff0c;后续排序按照从上至下或从右至左的顺序进行。 Orientation——…

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 …