1、Panel
还是本文开始提到的问题,只不过要将其中的FrameworkElement换为Panel。除了上面所提到的方法,Panel为我们提供了更加方便的实现方式。代码如下:

class MyElement : Panel { private Button _button = new Button() { Content = "I'm a Button!" }; public MyElement() { this.Children.Add(_button); } protected override Size MeasureOverride(Size availableSize) { if (this.VisualChildrenCount > 0) { UIElement child = this.GetVisualChild(0) as UIElement; Debug.Assert(child != null); // !Assert child.Measure(availableSize); return child.DesiredSize; } return availableSize; } protected override Size ArrangeOverride(Size finalSize) { Rect arrangeRect = new Rect() { Width = finalSize.Width, Height = finalSize.Height }; if (this.VisualChildrenCount > 0) { UIElement child = this.GetVisualChild(0) as UIElement; Debug.Assert(child != null); // !Assert child.Arrange(arrangeRect); } return finalSize; } }
之所以能这样做的原因是Panel已经替我们将如下几个工作封装在了UIElementCollection(Panel的Children属性)中:
-
AddVisualChildVisualChildCountGetVisualChild
2、VisualCollection
另外,在这个过程中,我们还可以使用一个叫做VisualCollection的类来作为所有 Visual Child的容器。这个容器构造的时候需要一个Visual类型的Parent,然后在添加、删除Visual Child的时候,它的相应方法(Add,Remove)就会帮助我们自动调用Parent的AddVisualChild和RemoveVisualChild方法。如此一来,我们的工作量又减少了。具体的实现代码很简单,这里就不贴了(总得动动脑子是不?)。
精彩图集精彩文章
热门标签
第三天
inner
SQLObject
jqeury
扩大分辨率
linux配置防火墙
设置宽度
print_r()
SendKeys参考文档
201
Dictionary
定长内存池
继续
mbstring扩展
gravity
from表单
PHP PHP随
写日志文件
line-height
通话记录
格式判断
函数参数
调用c
java验证码
序列号
Guard
磁盘配额
原生事件
应用
可拖动窗口
内存管理
uncheck
半角
记住用户名
gbk字符集
并发
deeplink
据库表结构
经验总结
删除服务
挂载目录
水平居中
AnalogCl
Windows服务器
最小值
table高级应用
python复制文件
函数实例
ViewFlipper
修改字符集
获得焦点
右外联
phpwind
css对联广告
文件类型
MessagePack
c++遍历
backtrak
表达式查
keyCode
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229