Access a Label Text which is inside a Border

Jassim Al Rahma 1,591 Reputation points
2023-05-07T21:14:14.7+00:00

Hi,

In below XAML and in codebehind, how can I access the LabelPhoneNumber in the Border's TelTapGestureRecognizer_Tapped?

<Border ClassId="TEL" BackgroundColor="White" Padding="0" Margin="5" HorizontalOptions="FillAndExpand">
<Border.StrokeShape>
    <RoundRectangle CornerRadius="20" Stroke="LightGray" />
</Border.StrokeShape>
<Border.GestureRecognizers>
    <TapGestureRecognizer Tapped="TelTapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />
</Border.GestureRecognizers>
    <Grid ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto">
        <Frame Grid.Column="0" Grid.RowSpan="2" IsClippedToBounds="True" BackgroundColor="Green" CornerRadius="20" Margin="15" Padding="0" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="40" HeightRequest="40">
            <Image Aspect="Center">
            <Image.Source>
                <FontImageSource FontFamily="FontSolid" Glyph="{x:Static local:IconFont.IconCall}" Color="White" Size="20" />
            </Image.Source>
            </Image>
        </Frame>

        <Label Grid.Column="1" Grid.Row="0" Text="Telephone" Margin="5,0,0,0" VerticalOptions="End" />
        <Label Grid.Column="1" Grid.Row="1" Text="{Binding PhoneNumber}" Margin="5,0,0,0" VerticalOptions="Start" />
    </Grid>
</Border>

Thanks,

Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,722 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jassim Al Rahma 1,591 Reputation points
    2023-05-08T04:51:56.2766667+00:00

    I found the solution:

    private void TelTapGestureRecognizer_Tapped(object sender, EventArgs e)
    {
    
        var border = (Border)sender;
        var grid = (Grid)border.FindByName("GridTelephones");
        var label = (Label)grid.FindByName("LabelPhoneNumber");
    
        string phoneNumber = label.Text;    
    }
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.