Search code examples
wpfxaml

Is there any risk using extremely high Zindex in WPF XAML?


I wonder if there is a risk of resource or performance when using super high ZIndex in a XAML view?

For exemple, is there a difference between this:

<Grid>
    <Rectangle Name="BigBoss" Canvas.ZIndex="1000000"/>
    <Rectangle Name="LitleBoss" Canvas.ZIndex="1000"/>
    <Rectangle Name="normalGuy" Canvas.ZIndex="2"/>
</Grid>

and this:

<Grid>
    <Rectangle Name="BigBoss" Canvas.ZIndex="2"/>
    <Rectangle Name="LitleBoss" Canvas.ZIndex="1"/>
    <Rectangle Name="normalGuy" Canvas.ZIndex="0"/>
</Grid>

I am not aware of the mechanic used behind ZIndex and how it is used to render the view but I assume that if there is a loop iterating every elements of the 1000000 "used" ZIndex, there would be clearly a performance issue.


Solution

  • I think there are not any performance risk. The ZIndex is a property for setting the priority of an item inside a wpf Panel (the same panel). If you set ZIndex=10000 to an item, it doesn't means there will be 10000 layers. It means that if there are any other item, with ZIndez lower, in the same panel, that overlap the first item, then this other item will be renderer behind the first item.

    By default the visual tree order has the same effect.