Controlling z-order using the ZIndex Property
The Composing a XAML Clip Art Scene posting showed how you could layer multiple drawing objects in an XAML file. The sample XAML file in that posting used the default z-order behavior of objects in a collection. One of the changes made in WPF Beta 2 is the introduction of the ZIndex attached property, which allows you to explicitly set the z-order of objects in the child collection of Panel-derived objects, such as Canvas, Grid, and StackPanel objects.
Using the ZIndex Property
Try running the following code in XAMLPad:
<Canvas Background="Ivory"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Canvas>
<Rectangle Height="60" Width="60" Fill="Gold"/>
<Rectangle Height="50" Width="70" Fill="Coral"/>
<Rectangle Height="40" Width="80" Fill="Teal"/>
<Rectangle Height="30" Width="90" Fill="Black"/>
</Canvas> <Canvas Canvas.Left="120">
<Rectangle Canvas.ZIndex="99" Height="60" Width="60" Fill="Gold"/>
<Rectangle Canvas.ZIndex="98" Height="50" Width="70" Fill="Coral"/>
<Rectangle Canvas.ZIndex="97" Height="40" Width="80" Fill="Teal"/>
<Rectangle Canvas.ZIndex="96" Height="30" Width="90" Fill="Black"/>
</Canvas> </Canvas>
Notice how the second set of Rectangle objects is layered in reverse z-order from the first set of Rectangleobjects:
The first set of Rectangle objects uses the default z-ordering rendering of objects, which is based on the position of the child object in the Canvas collection -- the first object in the collection is rendered first, then the second object, and so forth. The second set of Rectangle objects uses the ZIndex property to override the default z-ordering of objects in the collection.
In the second set of Rectangle objects, I want to ensure that the first object (the Gold-colored one) is the highest in the z-order, or closest to the foreground. I have defined its ZIndex value as "99" -- a purely arbitrary value. What is more important is that the value must be greater than any other child object ZIndex value in the Canvascollection. If you do not explicitly set the ZIndex value, it defaults to 0.
You can also set the ZIndex value to a negative value, such as "-99", which places the object even farther from the foreground. ZIndex values are aligned along the z-axis -- higher values are closer to the foreground; lower values are farther from the foreground.
Relationship of z-order to z-axis
Accessing the ZIndex Value at Run Time
To retrieve the value of the ZIndex property, use the GetZIndex method of the Panel-derived object:
int zOrder = Canvas.GetZIndex(object);
Likewise, to set the value of the ZIndex property, use the SetZIndex method:
Canvas.SetZIndex(object, (int)99);
A key performance issue is that changing the ZIndex property of a child object does not change its position within the collection. The ordering within the collection remains the same.
Prior to the introduction of the ZIndex property, you would have had to do the following to change the z-order of objects in a collection:
1) Remove the child object from the collection.
2) Re-insert the child object into the collection at the desired position.
In short, the addition of the ZIndex property is a much easier and more efficient way to change z-order!
Enjoy,
Lorin
Controlling z-order using the ZIndex Property的更多相关文章
- 自定义经纬度索引(非RTree、Morton Code[z order curve]、Geohash的方式)
自定义经纬度索引(非RTree.Morton Code[z order curve].Geohash的方式) Custom Indexing for Latitude-Longitude Data 网 ...
- Z Order of Controls in Delphi VCL
Get and set the Z Order of controls at runtime in Delphi VCL. If you are looking for a FireMonkey so ...
- Z Order of Controls in Delphi FireMonkey(Tom Yu的博客)
Get and set the Z Order of controls at runtime in Delphi FireMonkey. This is a follow on to my earli ...
- Z Order(Copy From WIN32.HLP)
The Z order of a window indicates the window's position in a stack of overlapping windows. This wind ...
- WPF Canvas
Canvas为容器控件,用于定位. 1.基本应用 <Border HorizontalAlignment="Left" VerticalAlignment="Top ...
- 有关CSS 定位中的盒装模型、position、z-index的学习心得
开始整体之前我需要说明两个概念: 第一个就是 一切皆为框 也就是说在HTML中的不管是是块级的还是内联的,都可以认为成块的,唯一的区别就是块的会独自占据一行 第二个文档流: 一个网页可以看作是 ...
- z-index 详解
Definition and Usage The z-index property specifies the stack order of an element. An element with g ...
- CSS z-index 属性的使用方法和层级树的概念
之前有一篇文章提到过z-index,我们知道只有在元素设置了position部位static时才生效,而且z-index也跟父元素有关系,今天就在ie7遇到类似问题,在网上查了一些资料,发现一篇好文章 ...
- 【转】CSS z-index 属性的使用方法和层级树的概念
文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...
随机推荐
- C语言内存对齐详解(1)
一.什么是字节对齐,为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的内存地址访问,这 ...
- HDU 4883 TIANKENG’s restaurant
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 解题报告:一家餐馆一天中有n波客人来吃饭,第 i 波 k 客人到达的时间是 s ,离开时的时间 ...
- 静态资源[org.springframework.web.servlet.PageNotFound]
springmvc 无法访问js.css.jpg等资源文件,tomcat启动报警告如下 [org.springframework.web.servlet.PageNotFound] - No mapp ...
- Linux统计文件个数
查看某个文件夹下的文件个数用ls列目录,用grep过虑,再用wc统计即可 用ls -l列出后, 每一行对应一个文件或目录, 如果第一个字母为’-'则为普通文件, 若为’d'则为子目录 + +grep过 ...
- 如何实现SSH断开后 进程仍然在后台运行
1.nohup命令功能:不挂断地运行命令,忽略HUP信号.语法:nohup command & 实例:nohup ping www.google.com & 转自: http://bl ...
- poj 1664
http://poj.org/problem?id=1664 题目是中文的,一个递归的题目 把每一次的苹果分为两类 Ⅰ:所以盘子都放一个,然后其他的在随便放: Ⅱ:有一个盘子没有放苹果: 这样下去的话 ...
- 如何调试lua脚本
首先感谢下ZeroBrane Studio. 这里拿cocos2dx/samples/Lua/HelloLua做例子来说明,其他的都是同样道理. 1.下载调试Lua所需的IDE,地址在这.有经济实力的 ...
- MFC dfs遍历文件
//如果涉及到大文件的遍历(大于4GB),可以将以下代码_finddata_t换成__finddata64_t,_findfirst换成_findfirst64,_findnext换成_findnex ...
- windows设置java环境变量
JAVA_HOME C:\Program Files\java\jdk1.6.0_38 path %JAVA_HOME%\bin; CLASSPATH .;%JAVA_HOME%\lib\dt.jar ...
- python基础——递归函数
python基础——递归函数 递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用 ...