ZIndex Changes Unexpectedly

Nathan Sokalski 4,126 Reputation points
2021-02-02T20:25:22.417+00:00

I have a DataTemplate containing several basic elements (a Grid containing Label(s) and Button(s)). Some of the Label(s) are on top of the Button(s) (they have the same Grid.Column & Grid.Row). However, at certain points when I click the Button the Label becomes hidden. If I place them in different cells (different Grid.Column & Grid.Row), they both remain visible. My conclusion from this is that the Button ends up covering the Label. The best solution I can come up with is to explicitly set the ZIndex in the XAML. In UWP, I did this using the attached property Canvas.ZIndex, but this does not seem to be available in Xamarin.Forms. What can I do?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-02-03T03:16:13.84+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Check Layout.RaiseChild(View) Method , it is used to send a child to the front of the visual stack.

    We can make button overlap the label in Gird using this method .

    A simple test on my side , it works as expected.

    Xaml

       <Grid x:Name="grid" >  
               <Grid.RowDefinitions >  
                   <RowDefinition Height="100"/>  
               </Grid.RowDefinitions>  
               <Grid.ColumnDefinitions>  
                   <ColumnDefinition Width="100"/>  
               </Grid.ColumnDefinitions>  
               <Button x:Name="button" Text="Click" Clicked="Button_Clicked" BackgroundColor="Red" />  
               <Label Text="123" BackgroundColor="Green" InputTransparent="True"/>  
    
           </Grid>  
    

    Code behind

       private void Button_Clicked(object sender, EventArgs e)  
               {  
                   grid.RaiseChild(button);  
               }  
    

    Screen shot

    //Before click
    63314-capture.png

    //After click
    63315-captu2re.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments