locked
Programmatically Change Z-Index of a Canvas RRS feed

  • Question

  •  

    I'm trying to get the following code to reduce the Z-Index of a selected Canvas when a button is clicked; making the selected Canvas appear under the other Canvas 

     if (currentZ > backgoundZ)
                    {
                       
                        for(int i = 0; i < partCanvas.Children.Count; i++)
                        {
                            if (partCanvas.Children(i).GetType() == typeof(Canvas))
                            {
                                Canvas tempCanvas = (Canvas)partCanvas.Children(i);
                                if (Canvas.GetZIndex(tempCanvas) == currentZ - 1)
                                {
                                    int  curZ = Canvas.GetZIndex(tempCanvas);
                                    Canvas.SetZIndex(tempCanvas, currentZ);
                                }
                            }
                        }
                        Canvas.SetZIndex(SelectedArtItem, currentZ - 1);
                        this.UpdateLayout();
                    } 


    this.UpdateLayout is referring to the main page in the application.  When the button to change the Z-Index is clicked, the Z-Index of both canvases contain the correct values, but the Canvas order isn't changed on screen. 
    Any help would be greatly appreciated.
     .

    Tuesday, February 2, 2010 10:09 PM

Answers

  • Hi,

        I guess you arranged your selectedArtCanvas as a child of outter canvas.

        Then no matter how you set the Z-index the child-parent relationship will not change.

        Seems that's why it's not working. (It's not the z-index make the selectedItem show upon the outter canvas)

        IMO, since you are using canvas as a root layout, you may arrange your layout via position and z-index to get a normal child-parent effect.  Then you can manipulate the whole canvas more easily

    Best Regards

    Wednesday, February 3, 2010 11:58 PM

All replies

  • In which container are you canvas? A Grid? A canvas?

    Just place your canvas into another canvas and change the z-index of the canvas you want to display. Dont change the zindex of all the children.

    Tuesday, February 2, 2010 10:21 PM
  • Thank you for the quick reply !! 

    The container is a canvas with many other canvases as children.  partCanvas is a child of another canvas and SelectedArtItem is a canvas that is a child of a canvas, inside partCanvas.  The hierarchy looks like this

    Canvas

         partCanvas

              Canvas

                    SelectedArtItem

    I need the swap Canvas and SelectedArtItem inside of partCanvas. 

    Dont change the zindex of all the children.
     

    I tried changing the z-index of SelectedArtItem without changing any other Z-Index values and I got the same result.

     

    Tuesday, February 2, 2010 10:30 PM
  • Hi,

        I guess you arranged your selectedArtCanvas as a child of outter canvas.

        Then no matter how you set the Z-index the child-parent relationship will not change.

        Seems that's why it's not working. (It's not the z-index make the selectedItem show upon the outter canvas)

        IMO, since you are using canvas as a root layout, you may arrange your layout via position and z-index to get a normal child-parent effect.  Then you can manipulate the whole canvas more easily

    Best Regards

    Wednesday, February 3, 2010 11:58 PM