How to get Label Width in Xaml?

Anonymous
2021-03-08T06:14:07.75+00:00

I want to set TabWidth equal to the width of label + some padding.
(I am new to xamarin.)

I tried below line but it prints -1.

TabWidth="{Binding Source={x:Reference TabLabel}, Path=WidthRequest}"

(Here, tab width is property of tabview of Xamarin Community ToolKit)

How to do this correctly? Is it possible to do without c#?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
Universal Windows Platform (UWP)
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
0 comments No comments
{count} votes

Accepted answer
  1. AryaDing-MSFT 2,916 Reputation points
    2021-03-08T07:56:13.117+00:00

    Hello,

    Welcome to Micorosoft Q&A!

    How to get Label Width in Xaml?

    Your code is correct, but if you have not specified the label's WidthRequest , the binding will not work. For your requirement, you need to bind Width property. Please try the following code.

    <Label  
    x:Name="Hello"  
    BackgroundColor="Gainsboro"  
    HorizontalOptions="Center"  
    Text="This Is Fist Line"  
    VerticalOptions="Center"  
    />  
      
    <Label  
    BackgroundColor="AliceBlue"  
    HorizontalOptions="Center"  
    Text="OtherOne"  
    WidthRequest="{Binding Source={x:Reference Hello}, Path=Width}" />  
    

    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

0 additional answers

Sort by: Most helpful

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.