I think I found the reason (well, sort of) and a workaround. There is a bug at:
https://github.com/xamarin/Xamarin.Forms/issues/6262
So I used the workaround of placing the Button inside a Grid, and now it seems good, hopefully the bug is fixed soon.
ZIndex Changes Unexpectedly
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?
-
Nathan Sokalski 4,111 Reputation points
2021-02-03T16:07:30.6+00:00
1 additional answer
Sort by: Most helpful
-
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
//After click
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.