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方法。如此一来,我们的工作量又减少了。具体的实现代码很简单,这里就不贴了(总得动动脑子是不?)。
精彩图集精彩文章
热门标签
INSERT_INTO_
获取主机名称
js改变背景图片
值不同的列
AVL树
exls
分割大文件
Httperr.Log
广播
通信模
Banner
类型
多态
原因分析
文件夹大小
object转数组
-bash
SetLocale
用户管理
阿里云主机
证书加密
console.log
三元表达式
字符串位置
组件
sae
velocity
ARP攻击
无法复制文件
bat
常常出现的
淡入
perl
jquery解析json
Slic
马赛克
增加账户
生命周期
$符
主键
youtube视频
上传视频
通过Socket
无用
httpclient
temp
屏幕快照
所有颜色值
python线程
Linux集群
采集器
系统分享
高效随机
yii
类变量
python函数调用
集合运算符
c语言http请求
配置心得
变量的值
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229