Controlling z-order using the ZIndex Property

时间:2021-09-20 08:14:43
 

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:

Controlling z-order using the ZIndex Property

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.

Controlling z-order using the ZIndex Property

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的更多相关文章

  1. 自定义经纬度索引&lpar;非RTree、Morton Code&lbrack;z order curve&rsqb;、Geohash的方式&rpar;

    自定义经纬度索引(非RTree.Morton Code[z order curve].Geohash的方式) Custom Indexing for Latitude-Longitude Data 网 ...

  2. 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 ...

  3. 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 ...

  4. Z Order(Copy From WIN32&period;HLP)

    The Z order of a window indicates the window's position in a stack of overlapping windows. This wind ...

  5. WPF Canvas

    Canvas为容器控件,用于定位. 1.基本应用 <Border HorizontalAlignment="Left" VerticalAlignment="Top ...

  6. 有关CSS 定位中的盒装模型、position、z-index的学习心得

    开始整体之前我需要说明两个概念: 第一个就是   一切皆为框  也就是说在HTML中的不管是是块级的还是内联的,都可以认为成块的,唯一的区别就是块的会独自占据一行 第二个文档流:  一个网页可以看作是 ...

  7. z-index 详解

    Definition and Usage The z-index property specifies the stack order of an element. An element with g ...

  8. CSS z-index 属性的使用方法和层级树的概念

    之前有一篇文章提到过z-index,我们知道只有在元素设置了position部位static时才生效,而且z-index也跟父元素有关系,今天就在ie7遇到类似问题,在网上查了一些资料,发现一篇好文章 ...

  9. 【转】CSS z-index 属性的使用方法和层级树的概念

    文章转自:CSS z-index 属性的使用方法和层级树的概念,另外加了一点自己的注释 CSS 中的 z-index 属性用于设置节点的堆叠顺序, 拥有更高堆叠顺序的节点将显示在堆叠顺序较低的节点前面 ...

随机推荐

  1. 史上最全JS表单验证封装类

    转自:http://www.cnblogs.com/linjiqin/p/3429919.html /************************************************* ...

  2. PN-Traniger

        首先先从Bezier说起: 一条直线上有两个端点,P0和P1,那么直线可以写成 y = kx+b ,其实也就是P(t) = (1-t)P0 + P1 (这是个插值函数),(小注,我时常把这两个 ...

  3. juit测试中报错:org&period;hibernate&period;HibernateException&colon; Unable to get the default Bean Validation factory

    org.hibernate.HibernateException: Unable to get the default Bean Validation factory 解决方法: 解决方案: 在hib ...

  4. python编程中的if &lowbar;&lowbar;name&lowbar;&lowbar; &equals;&equals; &&num;39&semi;main与windows中使用多进程

    if __name__ == 'main 一个python的文件有两种使用的方法,第一是直接作为程序执行,第二是import到其他的python程序中被调用(模块重用)执行. 因此if __name_ ...

  5. celery分布式异步框架

    1.什么是Celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组成,消息中间件( ...

  6. &lowbar;&lowbar;call&lowbar;&lowbar;方法和Flask中 first&lowbar;or&lowbar;404

    1.__call__方法: 在一个类的实例中,函数一般都是可调用的对象: __call__方法时魔法方法,该方法允许程序员创建可调用的对象(实例),默认情况下是不会触发,也就是说,大多数实例是不可被调 ...

  7. 卷积在图像处理中的应用(转自https&colon;&sol;&sol;medium&period;com&sol;&commat;irhumshafkat&sol;intuitively-understanding-convolutions-for-deep-learning-1f6f42faee1)

    直观理解深度学习的卷积 探索使他们工作的强大视觉层次   近年来强大且多功能的深度学习框架的出现使得可以将卷积层应用到深度学习模型中,这是一项非常简单的任务,通常可以在一行代码中实现. 然而,理解卷积 ...

  8. CEF C&plus;&plus;环境搭建

    第一步:下载CEF 到这里下载最新版本的CEF http://cefbuilds.com/ 下载解压之后,大概会看到如下图所示的文件 cefclient:是一个比较复杂的示例代码cefsimple:是 ...

  9. 基于mave的dubbo分别架构

    开始前,先看一下demo项目工程结构: 1.抽离接口 dubbo-api工程,根据业务抽离接口,deploy到mave nexus. public interface TestService { /* ...

  10. Python不能用于大型项目?人们对Python的十大误解

    Python 类型系统的特点是拥有强大.灵活的类型操作. *上对此作出的阐述. 而存在一个不争而有趣的事实是, Python 是比Java更加强类型的. Java 对于原生类型和对象区分了类型系 ...