无废话WPF系列7:WPF布局控件
2011-02-19 16:40 敏捷的水 阅读(2802) 评论(1) 编辑 收藏 举报一、 Grid
a. 单元格的宽度可以设置三类值
绝对值:double数值加单位后缀
比例值:double数值加一个星号*
自动值: auto,高度将有内部的控件的高度和宽度决定。
b. Grid可接受的宽度和高度的单位
1in=96px
1cm=(96/2.54)px
1pt=(96/72) px
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 | <Window x:Class= "DeepXAML.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local= "clr-namespace:DeepXAML" Title= "MainWindow" Height= "250" Width= "450" > <Window.Resources> <Style TargetType= "Button" > <Setter Property= "Margin" Value= "5" ></Setter> </Style> </Window.Resources> <Grid ShowGridLines= "True" > <Grid.RowDefinitions> <RowDefinition Height= "50" ></RowDefinition> <RowDefinition Height= "20" ></RowDefinition> <RowDefinition Height= "2*" ></RowDefinition> <RowDefinition Height= "*" ></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <Button Grid.Row= "0" >50</Button> <Button Grid.Row= "1" >20</Button> <Button Grid.Row= "2" >2*</Button> <Button Grid.Row= "3" >*</Button> <Button Grid.Row= "4" ></Button> </Grid> </Window> |

如果没有设置height,实际上这个height默认被设置为1*, 这里说一下星号(*),解析器会把左右比例值加起来作为分母,把每个比例值做为分子,乘以未被占用的像素数,这样就算出每一个具体的值。
我们假设总高为200,那么上面的2*=(2/(2+1+1))*(200-70)=(1/2)*130=65
另外可以像HTML表格一样使用RowSpan和ColumnSpan
2. StackPanel
控件从左向右或者从上向下排列控件,有一个Orientation枚举,还可以使用HorizontalAlignment和VerticalAlignment来进行对齐。
3. Canvas
使用横纵坐标绝对点定位,很好理解,使用Canvas.Left, Canvas.Top来定位
4. DockPanel
DockPanel使用Dock属性来定位,DockPanel.Dock枚举可取值Left, Top, Right, Bottom四个值,下一个元素会使用剩下方向的全部空间,所以控件摆放顺序会影响布局。Dock布局的一个好处是缩放不会改变相对位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <Window x:Class= "DeepXAML.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local= "clr-namespace:DeepXAML" Title= "MainWindow" Height= "250" Width= "450" > <Window.Resources> <Style TargetType= "Button" > <Setter Property= "Margin" Value= "5" ></Setter> </Style> </Window.Resources> <Grid ShowGridLines= "True" > <DockPanel > <Button DockPanel.Dock= "Top" >Top</Button> <Button DockPanel.Dock= "Left" Background= "Yellow" >Left</Button> <Button DockPanel.Dock= "Left" Background= "LemonChiffon" >Left</Button> <Button DockPanel.Dock= "Top" >Top</Button> <Button DockPanel.Dock= "Right" >Right</Button> <Button DockPanel.Dock= "Left" >Left</Button> <Rectangle Fill= "Crimson" ></Rectangle> </DockPanel> </Grid> </Window> |

5. WrapPanel
这个和StatckPanel类似,就是排不下的控件会新起一行或者一列。
扫码关注公众号,了解更多管理,见识,育儿等内容

出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)