首页 > Windows开发 > 详细

WPF笔记(2.9和2.10)——Layout

时间:2015-11-13 18:58:00      阅读:347      评论:0      收藏:0      [点我收藏+]
原文:WPF笔记(2.9和2.10)——Layout

 2.9讲的是,如果内部设定超过容器大小,怎么办?
StackPanel会裁剪越界部分
DockPanel和Grid会智能判断,从而决定换行。

2.10 自定义布局容器
自定义容器要实现两个方法MeasureOverride和ArrangeOverride,并保证遍历其下的所有子控件,使他们都执行Measure和Arrange方法。

技术分享using System;
技术分享
using System.Windows.Controls;
技术分享
using System.Windows;
技术分享
技术分享
namespace CustomPanel {
技术分享    
public class DiagonalPanel : Panel {
技术分享
技术分享        
protected override Size MeasureOverride( Size availableSize ) {
技术分享            
double totalWidth = 0;
技术分享            
double totalHeight = 0;
技术分享
技术分享            
foreach( UIElement child in Children ) {
技术分享                child.Measure( 
new Size( double.PositiveInfinity,
技术分享                                         
double.PositiveInfinity ) );
技术分享                Size childSize 
= child.DesiredSize;
技术分享                totalWidth 
+= childSize.Width;
技术分享                totalHeight 
+= childSize.Height;
技术分享            }

技术分享
技术分享            
return new Size( totalWidth, totalHeight );
技术分享        }

技术分享
技术分享        
protected override Size ArrangeOverride( Size finalSize ) {
技术分享            Point currentPosition 
= new Point( );
技术分享
技术分享            
foreach( UIElement child in Children ) {
技术分享                Rect childRect 
= new Rect( currentPosition, child.DesiredSize );
技术分享                child.Arrange( childRect );
技术分享                currentPosition.Offset( childRect.Width, childRect.Height );
技术分享            }

技术分享
技术分享            
return new Size( currentPosition.X, currentPosition.Y );
技术分享        }

技术分享    }

技术分享}

 

WPF笔记(2.9和2.10)——Layout

原文:http://www.cnblogs.com/lonelyxmas/p/4962826.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!