Search code examples
c#silverlightwindows-phone-8z-order

Change z order of image controls at runtime windows phone


I have several image controls placed on a canvas. I want to change the z order of a selected image control when user click on the app bar button.

In the image control class I have

    public int ImageZOrder 
    { 
        get
        {
            return _imageZOrder;            
        }
        set 
        {
            _imageZOrder = value;
            Canvas.SetZIndex(ImageControl, _imageZOrder);
        }

    }

And in the app bar

    private void appBarFront_Click(object sender, EventArgs e)
    {
        currentCharObject.ImageZOrder += 1;
    }

I was expecting to see the selected image is brought to the front of other image controls immediately. However the above code does not work. Obviously I am missing something. Do I have to redraw the image controls on the canvas with the new z order value?

Update 1

In the XAML I have the following. Each image was added as children to cvsNote

    <Grid x:Name="GridCanvas" Grid.Row="0">
        <Canvas x:Name="cvsNote" />
    </Grid>

Update 2 I just want to add that the 'image control' contains in a particular class where it is just one of the class many properties.


Solution

  • You can change the ZIndex in code.

    Assuming this xaml:

    <Canvas Grid.Row="1" Tap="gridTapped">
        <Image x:Name="ImgA" Source="Assets/A.png" />
        <Image x:Name="ImgB" Source="Assets/B.png" />
        <Image x:Name="ImgC" Source="Assets/C.png" />
    </Canvas>
    

    You could change which image is on top with the following:

    private int TopZindex = 10;
    
    private void gridTapped(object sender, GestureEventArgs e)
    {
        var rand = new Random();
        switch (rand.Next(0, 3))
        {
            case 0:
                Canvas.SetZIndex(this.ImgA, ++TopZindex);
                break;
            case 1:
                Canvas.SetZIndex(this.ImgB, ++TopZindex);
                break;
            case 2:
                Canvas.SetZIndex(this.ImgC, ++TopZindex);
                break;
        }
    }