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

WPF布局系统深度解析:Panel原理与MeasureArrange机制 WPF布局系统是基于Panel基类的递归渲染框架,其核心在于MeasureOverride与ArrangeOverride两个生命周期方法对可用空间(availableSize)和期望尺寸(DesiredSize)的协同计算。该机制决定了控件如何响应父容器约束、如何参与尺寸协商、以及为何出现截断、错位、不填满等典型问题。理解这一底层原理,不仅能精准调试Canvas定位异常、Grid列宽紊乱、StackPanel文字截断等高频故障,更能指导高性能布局选型——如高帧率场景优先Canvas,表单类界面首选Grid 阅读详情

面板是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>

在这里插入图片描述

【机器学习入门】1.4 吃透这些基本术语,才算真正入门 本文通过"挑西瓜"的生活案例,形象解释了机器学习的基本术语。样本对应单个西瓜,特征如色泽、根蒂等是观察维度,特征值是其具体表现,标签是西瓜的好坏判断。数据集是样本集合,特征向量是样本的数学表达,特征空间是所有可能的样本组合。文章强调数据集需划分为训练集、验证集和测试集,以评估模型的泛化能力。最后整理了术语对照表,并解答了常见问题。这些基础概念是理解机器学习的关键,建议读者重点掌握。 阅读详情

相关推荐

YOLOv11改进策略【损失函数篇】| 通过辅助边界框计算IoU提升检测效果(Inner_GIoU、Inner_DIoU、Inner_CIoU、Inner_EIoU、Inner_SIoU)

为了弥补现有 IoU 损失在不同检测任务中泛化性弱和收敛速度慢的问题,·Inner-IoU·通过引入比例因子 “ratio” 来控制辅助边界框的尺度大小,利用不同尺度的辅助边界框来计算损失,从而加速边界框回归过程。

Limiiiing的博客 3844

wpf加载winform窗体

using System.Windows.Controls; using System.Windows.Forms.Integration; using Fibonacci; namespace WPFHost {     /// summary>     /// Interaction logic for Page1.xaml     /// /summary>

devgis 3317

摩托罗拉 GM300 维修手册

摩托罗拉GM300对讲机的维修手册。非常实用

WPF 内建面板学习总结(一)

面板能承载其他子元素,并可设置子元素的大小、位置。DockPanelCanvasStackPanel、Grid是WPF中主要的内建面板,这些面板类都位于System.Windows.Controls.Panel。

yeying2009的博客 845

WPF入门教程系列六——布局介绍与Canvas(一)

具体如下图,要实现控件0的布局,那么先要实现0的子控件 01,02...的布局,要实现01的布局,那么得实现01的子控件001,002...的布局,如此循环直到子控件的布局完成后,再完成父控件的布局, 最后递归回去直到递归结束,这样整个布局过程就完成了.从这篇文章开始是对WPF中的界面如何布局做一个较简单的介绍,大家都知道:UI是做好一个软件很重要的因素,如果没有一个漂亮的UI,功能做的再好也无法吸引很多用户使用,而且没有漂亮的界面,那么普通用户会感觉这个软件没有多少使用价值。获取或设置元素的标识名称。

Gefangenes的博客 1816

WPF中5种内建面板CanvasStackPanelWrapPanelDockPanel、Grid分析

CanvasStackPanelWrapPanelDockPanel和Grid是WPF中主要的5种内建面板,这些面板类都位于System.Windows.Controls命名空间下。 WPF内建面板之——Grid Grid是最通用的面板,它可以让你在一个多行、多列的表中排列子元素,而不依靠包装,提供了许多特性来有效地可能告知行和列。 用Grid实现类似于Visual Studio的启动界面:...

邻家小哥哥的博客 3047

WPF中的StackPanelWrapPanelDockPanel

一、StackPanel StackPanel是以堆叠的方式显示其中的控件 1、可以使用Orientation属性更改堆叠的顺序 Orientation="Vertical" 默认,由上到下显示各控件控件在未定义的前提下,宽度为StackPanel的宽度,高度自动适应控件中内容的高度 1: 2: Button A 3: Button B 4: B

不积跬步,无以至千里;不积小流,无以成江海 3万+

wpf】05 几种容器动态创建控件的对比

从上面代码实现的效果来看,对于动态生成的控件可以分成两种可能性:一是固定数量和种类的控件生成;二是不固定数量和种类的控件生成。从上面的实现效果可以明显看出,不固定数量的控件生成很容易超出容器范围,从而无法看到,那这里能够胜任此功能的只有使用ScrollViewer和其他容器的组合实现,效果是比较好的。如下:wpf控件的灵活组合,还有很多值得深入学习的地方,在使用的过程中,逐渐进步。

kewaqi618的博客 2089

WPF 的内部世界(控件与布局)

目录 一、控件与布局 前言 为什么要写WPF呢? 我一开始算是比较抵触WPF的,因为用的人少吗。感觉都是窗体应用能和Winform有什么区别。可是我错了,非常感谢我的讲师,给我推荐刘铁猛的《深入浅出WPF》,让我了解到了WPF的魅力——数据驱动UI 。 所以,这么优秀的框架,我想写下来,都知道WPF开发人员非常少,以至于大部分教程视频都是10年前的。我记录下来,不是为了什么,是真的喜欢WPF,那种“怪诞不经”的感觉。 一、UI布局 俗话说:“人靠衣装马靠鞍” 什么意思呢?意思是人穿上一身得体的衣服,就会显.

rizon886的博客 441

WPF控件和布局

WPF控件和布局   WPF控件和布局,根据刘铁猛《深入浅出WPF》书籍讲解内容,主要记录控件和布局的原理,如果有不足的地方,请大牛们键盘下留情--轻喷!如果还算有用,请给点动力,支持一把! 一、WPF里的控件 1.1 控件的实质   我们先从UI上分析,UI的功能是让用户观察和操作数据,为了能显示数据和响应用户的操作通知程序(通过事件来通知,如何处理事件又是一系列的算法),所以控件就是显...

Junit的博客 275

WPF快速入门系列(1)——WPF布局概览

WPF快速入门系列(1)——WPF布局概览 一、引言   关于WPF早在一年前就已经看过《深入浅出WPF》这本书,当时看完之后由于没有做笔记,以至于我现在又重新捡起来并记录下学习的过程,本系列将是一个WPF快速入门系列,主要介绍WPF中主要的几个不同的特性,如依赖属性、命令、路由事件等。   在正式介绍之前,我还想分享下为什么我又要重新捡起来W...

fgmah2000的博客 237

Wpf中几大布局容器的一个总结

2020/11/17 文章目录2020/11/17前言一、分别介绍几大容器1.DockPanel(泊靠式面板)2.WrapPanel(环绕面板)3.Canvas(画布)4.StackPanel(栈式面板)4.Grid(网格面板)二、几种布局之间的总结,其中的特点与应用场景总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。 提示:以下是本篇文章正文内容,下面案例可供参考 一、分别介

xiangxue3333的博客 1434

WPF 控件专题 Canvas 控件详解

WPF 控件专题 Canvas 控件详解

^@^lemon tea^@^ 2421

WPF Panel的性能分析

大家知道WPF有多种Panel,如Canvas,Grid,StackPanel,DockPanel,WrapPanel,VirtualizingPanel等。 在一些场景下可以选择任何一种或多种Panel实现一种效果。本文谈一下在同一场景下使用哪种Panel性能会更好。   新建一个WPF项目,各放置Stackpanel,Canvas,Grid,看下所占的内存,如图1,2,3                      图1 Grid                     图2 Canvas

dong123ohyes的专栏 767

WPF-布局-《一》

1.StackPanel控件,会充满空间 水平布局 <StackPanel Orientation="Vertical"> <Button Content="Button"/> <Button Content="Button"/> <Button Content="Button"/> </StackPanel> 垂直布局 <StackPan

故里2130 7442

如何在WPF中获取控件以填充可用空间?

Some WPF controls (like the Button ) seem to happily consume all the availible space in its' contai

CHCH998的博客 2076

WPF-面板的三种形式

** WPF-面板的三种形式 ** 一、DockPanel:泊靠式面板 DockPanel定义一个区域,在此区域中,您可以使子元素通过描点的形式排列,这些对象位于 Children 属性中。 DockPanel会对每个子元素进行排序,并将根据指定的边进行停靠,多个停靠在同侧的元素则按顺序排序。 在DockPanel中,指定停靠边的控件,会根据定义的顺序占领边角,所有控件绝不会交叠。 默认情况下,后添加的元素只能使用剩余空间,无论对DockPanel的最后一个子元素设置任何停靠值,该子元素都将始终填满剩余的空

weixin_50180191的博客 889

WPF UI布局之概述

在线演示:http://v.youku.com/v_show/id_XNzA5NDk2Mjcy.html 清晰版视频+代码下载:http://115.com/lb/5lbeer0m9lad 一、简介 本篇对WPF的布局控件做一个初步的概览,并分别演示Grid、StackPanelCanvasDockPanelWrapPanel五个布局控件。。 主要内容包括: 1、UI布局的方式

光脚丫思考的专栏 4701
上一篇: 在vs2017中尝试c#单元测试,Moq(二)
下一篇: 对wpf 的入门记录总结----基础控件textblock、label、 TextBox、CheckBox、RadioButton、PasswordBox、Image
dark_tone
博客等级 码龄10年 315粉丝 256原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值