如何在Xamarin中将一个元素的宽度设置为另一个元素的宽度?

时间:2019-12-05 09:42:21

标签: xaml xamarin xamarin.forms

我正在使用框视图在Xamarin中的某些文本下划线,我想将框视图的宽度设置为标签的宽度

3 个答案:

答案 0 :(得分:3)

标签具有属性 TextDecorations 。您可以将其设置为 下划线 ,以在不使用BoxView的情况下对文本加下划线。

    <Label Text="Your Text" TextDecorations="Underline">

答案 1 :(得分:2)

为Label定义WidthRequest并将其绑定到Box视图,如下所示。

<StackLayout Padding="0,10" HorizontalOptions="Center" VerticalOptions="Center" Orientation="Horizontal">
    <Button x:Name="BtnSend" Clicked="BtnSend_Clicked" Text="Send" WidthRequest="150" />

    <Label Text="Hello" BackgroundColor="Yellow" WidthRequest="{Binding WidthRequest, Source={x:Reference BtnSend}}"  />
</StackLayout>

答案 2 :(得分:0)

下面的代码将完全提供您想要的内容。

<StackLayout HorizontalOptions="Start">
    <Label x:Name="textLabel" Text="This is an underlined text" WidthRequest="160" />
    <BoxView Color="Gray" HeightRequest="0.5" WidthRequest="{Binding WidthRequest, Source={x:Reference textLabel}}"  />
</StackLayout>

enter image description here