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 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...
随机推荐
- [Effective JavaScript 笔记]第54条:将undefined看做“没有值”
undefined值很特殊,每当js无法提供具体的值时,就会产生undefined. undefined值场景 未赋值的变量的初始值即为undefined. var x; x;//undefined ...
- 使用Cydia Substrate 从Native Hook Android Java世界
这里介绍了如何使用Cydia Substrate Hook安卓Java世界.这篇文章介绍如何从Native中Hook 安卓Java世界. 手机端配置见之前文章. 一.建立工程 建立一个Android工 ...
- nginx学习(二):初识配置文件
nginx的配置文件默认在nginx安装目录中的conf子目录中,主配置文件为nginx.conf, root@mgmserver conf]# pwd/usr/local/nginx/conf一.配 ...
- 极客教学:如何使用树莓派击落&劫持无人机
本教程的目的是帮助大家理解如何研究未受保护的无线通信的安全风险所在,同时我们希望大家不要对技术进行滥用.我们这里采用的例子是一个流行的无人机模型:Parrot AR.Drone 2.0. 四轴无人机能 ...
- mysql如何用order by 自定义排序
mysql如何用order by 自定义排序 id name roleId aaa bbb ccc ddd eee ,MySQL可以通过field()函数自定义排序,格式:field(value,st ...
- C#中Const和Readonly的区别
const 的概念就是一个包含不能修改的值的变量.常数表达式是在编译时可被完全计算的表达式.因此不能从一个变量中提取的值来初始化常量.如果 const int a = b+1;b是一个变量,显然不能再 ...
- Android studio 添加依赖
以前添加依赖总是到github上下载源码,再添加源码到module的依赖当中,其实在studio中,应该使用maven库. 比如在github上看到了sliding-menu这个项目,就应该到mave ...
- mysql 我的学习
安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...
- HDU 5510 Bazinga (字符串匹配)
题目:传送门. 题意:t 组数据,每组 n 个串,对于第 i 个串如果存在 1 到 i-1 中的某个串不是 i 的子串,那么这个第 i 个串符合题意,求 i 的最大值. 题解:KMP,AC自动机也可以 ...
- vs c++中读取数据流并存储
ifstream in("test.txt"); vector<string> vs; string s; while(!in.eof()) { in>>s ...