interesting Z Index behaviour


  • I've noticed zindex behaviour little different when created thru XAML vs c# code.

    I've created Canvas and added rectangle and any other UI element added after that in the XAML the other UIelement gets the mouse click and it's rendered on top of rectangle.

    contrarily in the C# i've to explicitly add Rectangle ZIndex needs to be set to -1 and hittest needs to be set to false as well. Little puzzled.

    Here's the sample XAML

    <Canvas x:Name="left" Width="600" Height="425">


      <PathGeometry x:Name="leftSheetclipPath">




    <Rectangle x:Name="rect" Fill="#55FF0000" Canvas.Left="0" Canvas.Top="0" Width="300" Height="995" RenderTransformOrigin="1.0,0.8">


    <RotateTransform x:Name="Angle" Angle="0"/>




    C# approach

    rect = new Rectangle();

    rect.Width = 600;

    rect.Height = 425;

    rect.Fill =
    new SolidColorBrush(Color.FromArgb(100, 100, 50, 200));

    rect.SetValue(Canvas.LeftProperty, 0.0);

    rect.SetValue(Canvas.TopProperty, 0.0);

    rect.RenderTransformOrigin = new Point(1.0, 0.8);

    Canvas.SetZIndex(rect, -1);

    rect.IsHitTestVisible = false;


    Sunday, February 28, 2010 9:32 AM