Rendering Order with the Silverlight Z-Index Property

The order in which elements are rendered in Silverlight is determined firstly by where they appear in the visual object hierarchy and secondly by their Z-Index property.  Understanding this can be difficult at first but it is no different to the way browsers render plain HTML elements.

As I've said at the start the object hierarchy is the first part of the puzzle.  By object I mean a Canvas, Grid or StackPanel (any element derived from Panel), any object that can contain elements such as the TextBlock, Rectangle, Line etc.

<Canvas x:Name="parentCanvas"
        xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Loaded="Page_Loaded"
        x:Class="ZIndex.Page;assembly=ClientBin/ZIndex.dll"
        Width="640"
        Height="480"
        Background="White"
        >
  <Canvas>
    <Rectangle Canvas.Left="40" Canvas.Top="40" Fill="#0000ff" Width="100" Height="100" />
    <Rectangle Canvas.Left="60" Canvas.Top="60" Fill="#4444ff" Width="100" Height="100" />
    <Rectangle Canvas.Left="80" Canvas.Top="80" Fill="#8888ff" Width="100" Height="100" />
    <Rectangle Canvas.Left="100" Canvas.Top="100" Fill="#bbbbff" Width="100" Height="100" />
  </Canvas>

  <Canvas Canvas.Top="200" Canvas.Left="200">
    <Rectangle Canvas.Left="0" Canvas.Top="0" Fill="#ff0000" Width="100" Height="100" Canvas.ZIndex="2" />
    <Rectangle Canvas.Left="20" Canvas.Top="20" Fill="#ff4444" Width="100" Height="100" />
    <Rectangle Canvas.Left="40" Canvas.Top="40" Fill="#ff8888" Width="100" Height="100" />
    <Rectangle Canvas.Left="60" Canvas.Top="60" Fill="#ffbbbb" Width="100" Height="100" />
  </Canvas>
</Canvas>

In the screenshot below you can see the first set of Rectangles (Blue) are rendered in order of appearance.  This is the default behaviour that Silverlight applies to elements when rendering them.

For the second set of rectangles (Red) the first rectangle is visible above the others, this is because of the Canvas.ZIndex setting.  Try playing with the settings to see what the effects are.

Screenshot showing two sets of rectangles

Using Canvas.ZIndex in a Grid

Just as you can apply the Canvas.ZIndex property to elements in a Canvas you can also use the Canvas.ZIndex property for elements in a Grid as well to control depth rendering order.

Your Comments

Pankaj posted

Great Demostration....


rookieleo posted

Very very straightful and clear, thank you


Adriana posted

hello,


I have to manage the zindex of the children of a container which inherites from panel, but I don't know how... could add some dynamic examples?


By the way, thanks for the explanation :-)


Ashish posted

Thank you very much


Ashish posted

Thank you very much


Huzefa Mala posted

Please continue the good work


Camil AK posted

10X FOR THIS USEFUL POST AND EASY 2 UNDERSTAND


Jawahar posted

Nice expalanation about Z-index... easy to understand.


Raghuraman posted

Thanks for posting about silverlight Canvas.ZIndexProperty found the image displayed very helpful to easily understand.



Regards



KRK


prabhash posted

Thank you for sharing this Nice Information


 

Post your Comments

Rate this: 

1 Star 2 Star 3 Star 4 Star 5 Star
40 Ratings / 3.1 Average
 
Latest Games
Car Parking
Jan 16, 2016
Plays: 37

Car Parking - Free iPhone, iPad, Android Physics Game ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating / 5 Average
Trash It
Jan 11, 2016
Plays: 60

Trash It - Free iPhone, iPad, Android Physics Game ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

1 Rating / 5 Average
Sky Fly
Jan 11, 2016
Plays: 58

Sky Fly - Free iPhone, iPad, Android Shooting Game ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating / 5 Average
Professor Snappy
Jan 11, 2016
Plays: 31

Professor Snappy - Free iPhone, iPad, Android Puzzle Game ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating / 5 Average
Monster Match Saga
Jan 10, 2016
Plays: 39

Monster Match Saga - Free iPhone, iPad, Android Match 3 Game ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

1 Rating / 5 Average
Fly Bird Fly
Jan 10, 2016
Plays: 45

Fly Bird Fly - Free iPhone, iPad, Android Flappy Bird Game ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating / 5 Average
Life In One
Jan 10, 2016
Plays: 51

Life In One - Free iPhone, iPad, Android Strategy Game ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

1 Rating / 5 Average
X Pool
Jan 02, 2016
Plays: 102

X Pool - Free iPhone, iPad, Android Game ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

1 Rating / 5 Average
Fruit Slicer
Jan 02, 2016
Plays: 42

Fruit Slicer - Free iPhone, iPad, Android Game ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

2 Ratings / 5 Average
Alien Defense
Jan 02, 2016
Plays: 56

Alien Defense - Free iPhone, iPad, Android Game ScreenshotDefend against the oncoming waves of enemy alien attack. Place your gun turrets in strategic positions to destroy the attackers!

1 Rating / 5 Average
Car Dodge Extreme
Jan 01, 2016
Plays: 57

Car Dodge Extreme - Free iPhone, iPad, Android Game ScreenshotRace the wrong way up a busy road collecting the Gold and Silver coins whilst dodging the oncoming traffic!

1 Rating / 5 Average
Tic Tac Toe XL
Jan 01, 2016
Plays: 43

Tic Tac Toe XL - Free iPhone, iPad, Android Game ScreenshotPlay the classic game of Tic Tac Toe on your large screen tablet and phones. Play against a skilled computer opponent who will try to beat you at every turn.

2 Ratings / 5 Average
Submit a Game Here!