WPF笔记(1.4 布局)——Hello,WPF!

本文深入探讨了WPF中的Grid面板布局技术,通过示例代码详细展示了如何使用Grid创建灵活且高效的布局结构。从基本用法到高级应用,全面覆盖Grid面板在界面设计中的多功能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文: WPF笔记(1.4 布局)——Hello,WPF!

这一节只是第2章的引子。
布局要使用Panel控件,有四种Panel,如下:
DockPanel,就是设置停靠位置布局模型。
StackPanel,提供一个从左至右或从上至下放置内容的堆栈模型。
Grid,提供一个允许进行 行/网格定位的模型。可使用表格。
Canvas,可精确定位。

其中,Grid是最常用的,vs2005自动生成的Page和window都默认带有这个标签:

None.gif Example  1 - 25 . A sample usage of the Grid panel
None.gif
< Window dot.gif >
None.gif  
< Grid >
None.gif    
< Grid.RowDefinitions >
None.gif      
< RowDefinition  />
None.gif      
< RowDefinition  />
None.gif      
< RowDefinition  />
None.gif    
</ Grid.RowDefinitions >
None.gif    
< Grid.ColumnDefinitions >
None.gif      
< ColumnDefinition  />
None.gif      
< ColumnDefinition  />
None.gif      
< ColumnDefinition  />
None.gif    
</ Grid.ColumnDefinitions >
None.gif    
< Button Grid.Row = " 0 "  Grid.Column = " 0 "  Grid.ColumnSpan = " 2 " > A </ Button >
None.gif    
< Button Grid.Row = " 0 "  Grid.Column = " 2 " > C </ Button >
None.gif    
< Button Grid.Row = " 1 "  Grid.Column = " 0 "  Grid.RowSpan = " 2 " > D </ Button >
None.gif    
< Button Grid.Row = " 1 "  Grid.Column = " 1 " > E </ Button >
None.gif    
< Button Grid.Row = " 1 "  Grid.Column = " 2 " > F </ Button >
None.gif    
< Button Grid.Row = " 2 "  Grid.Column = " 1 " > H </ Button >
None.gif    
< Button Grid.Row = " 2 "  Grid.Column = " 2 " > I </ Button >
None.gif  
</ Grid >
None.gif
</ Window >
这段程序产生一个3x3表格。注意,先定义行格式,再定义列格式,最后是往单元格放入button。

None.gif Example  1 - 26 . Arranging an image and text  in  a grid
None.gif
< Button Width = " 100 "  Height = " 100 " >
None.gif  
< Button.Content >
None.gif    
< Grid >
None.gif      
< Grid.RowDefinitions >
None.gif        
< RowDefinition  />
None.gif        
< RowDefinition  />
None.gif      
</ Grid.RowDefinitions >
None.gif      
< Image Grid.Row = " 0 "  Source = " tom.png "   />
None.gif      
< TextBlock
None.gif        Grid.Row
= " 1 "
None.gif        HorizontalAlignment
= " Center " > Tom </ TextBlock >
None.gif    
</ Grid >
None.gif  
</ Button.Content >
None.gif
</ Button >
这段程序是在图片下面加了一行Caption,也是用的Grid下表格排版。

以上两个例子都有 Grid.Row=1这样的语法——attached-property牵连属性。即在Grid内部定义(),在外部控件Button中指定属性值。
牵连属性的用途,事先不一定用Button填充单元格,这样对所有控件就有任意性——暂时这么想,因为没看第二章。
posted on 2015-11-13 17:18 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/4962711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值