1、Panel
还是本文开始提到的问题,只不过要将其中的FrameworkElement换为Panel。除了上面所提到的方法,Panel为我们提供了更加方便的实现方式。代码如下:
Code [http://www.xueit.com]
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方法。如此一来,我们的工作量又减少了。具体的实现代码很简单,这里就不贴了(总得动动脑子是不?)。
精彩图集精彩文章
热门标签
移动网络数据
元素拖动
新浪微博api
Linux用户管理
nowdoc
logging
date命令
mysql字符串
相同后缀
国家气象局
无环图
高级编程
枚举
当前
CD-KEY
拿来
配置jdk
开发工具
层次查询
未读
大小写敏感
R3
java反序列化
空行
分割文件
PHP分词
保护硬盘
拼合生成器
统计总行数
创建视图
windows误删
php 将字
游戏
透明窗体
聊天命令
self
android网络编程
照片伸缩
文件大小
excel生成报表
UbuntuSamba
触发表
二进制流
HTML表单
语法特性
超实
Prim算法
导航问题
NGEN
两行字符
AjaxFileUplo
XHTML
运行机制
前导0
setInterval
标签
301
D3.js
另一
Mempodipper本
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

