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方法。如此一来,我们的工作量又减少了。具体的实现代码很简单,这里就不贴了(总得动动脑子是不?)。
精彩图集精彩文章
热门标签
追加文件内
当前位置
偏移量
items()
bat
Loader
升序排序
WebsitePanel
Mygui
jqueryhighch
清除标签
Moxa
文件引用
data
主要
文件编码
调用cmd
decorator
分栏显示
保存图
feedparser
rhythmbox
DAYOFMONTH
单位
ProgressBar
ACL权限
wrapIn
oracle还原
sequence重置
断开连接
c语言数组排序
不固定参数
blob
mysql:
天气接口
LANG
vnc服务器配置
X10.9安装
overflow:hid
indexOf
mutimap
Y
http-post
越界
round()
跨表复制
定时跳转
16进制
BufferedImag
数组形式
showModalDia
表死锁
骰子
ajaxSubmit
指定行
vim
测试入门
汇总
原始套接字
java读取文件
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229