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方法。如此一来,我们的工作量又减少了。具体的实现代码很简单,这里就不贴了(总得动动脑子是不?)。
精彩图集精彩文章
热门标签
php引用传值
map()
吓倒
定要
程连接慢
报错
W3C
ip数据
MySQL数据库引擎
取模运算
JDBC示例
10种
c语言知识点
IBM
误删数据
页面乱码
REPLACE
vbs类
Moxa
python发邮件
自动播放
延时处
row_count
$_SESSION
文章模块
addClass
通讯录
file,value
重复元素
时间格式化
0x8007007e
solr创建索引
梯形法
ORA-01114
stl算法
前两个页面
模版方法
视频播放
编码格式
php php短网
lambda
执行shell命令
urlencode
IIS8.5
编译python
保留
细说
int*p[]
epoll
导出插入
删除自身
jquery插件
开3389
图片旋转
Magento
int(*p)[]
countdownlat
漂亮的表格效
用此
点击链接
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

