简体   繁体   中英

How is it that setting items with the same Zindex doesn't raise an error?

I have simple Canvas in a Mainwindow of WPF .

First, I'm adding a rectangle of height and width of 100 and Zindex of 0 to the canvas. After that, I'm adding a similar rectangle of red color and Zindex of 0 to the same canvas.

This is my code:

        var R1 = new Rectangle();
        R1.Height = 100;
        R1.Width = 100;
        R1.Fill = Brushes.Blue;
        Canvas.SetLeft(R1, 100);
        Canvas.SetTop(R1, 100);
        Canvas.SetZIndex(R1, 0);
        Can1.Children.Add(R1);

        var R2 = new Rectangle();
        R2.Height = 100;
        R2.Width = 100;
        R2.Fill = Brushes.Red;
        Canvas.SetLeft(R2, 100);
        Canvas.SetTop(R2, 100);
        Canvas.SetZIndex(R2, 0);
        Can1.Children.Add(R2);

How is it that the place for R2 is taken, and that the element is added to the canvas without any error being raised? What happens is the R2 is placed over R1, but I didn't set the Zindex of it to be bigger than 0.

How is it that the place for R2 is taken, and that the element is added to the canvas without any error being raised?

Elements can be placed in parent container with same z-index . If you won't set any z-index on it, by default they are added with same z-index. Hence, no error.

What happens is the R2 is placed over R1, but I didn't set the Zindex of it to be bigger than 0.

If you don't set higher z-index on new child element then last added child always win ie it will be placed over the element with same z-index . Hence R2 will be shown above R1 in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM