Xamarin Xaml将按钮的高度绑定到Label的高度

时间:2019-03-26 23:17:20

标签: xaml xamarin button label

我在网格中有一个标签和一个按钮。我正在尝试使按钮的高度与标签相匹配。

当前XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <!-- vessel label and button -->
    <Label StyleClass="headerSub" Grid.Column="0" x:Name="currentVesselLabel" 
           Text="huidig voertuig:"/>
    <Button Grid.Column="1" Text="selecteer voertuig" 
            BindingContext="{x:Reference currentVesselLabel}"
            HeightRequest="{Binding Path=HeightRequest}"
            ClassId="selectVesselButton" x:Name="selectVesselButton"/>
</Grid>

我尝试将BindingContext设置为标签,并将按钮的HeightRequest属性绑定到Label的HeightHeightRequest属性。但是,两者都不起作用。如何将按钮的高度绑定到标签的高度?

1 个答案:

答案 0 :(得分:0)

原因:

似乎您忘记设置行高了,当网格中只有一行时,默认行高将被设置为屏幕的高度。

解决方案:

设置行的高度。请参考以下代码:

<Grid>

    <Grid.RowDefinitions>

        <RowDefinition Height="Auto"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <!-- vessel label and button -->
    <Label StyleClass="headerSub" Grid.Column="0" x:Name="currentVesselLabel" 
       Text="huidig voertuig:" VerticalTextAlignment="Center" HeightRequest="60"/>
    <Button Grid.Column="1" Text="selecteer voertuig" 
        BindingContext="{x:Reference currentVesselLabel}"
        HeightRequest="{Binding Path=HeightRequest}"
        ClassId="selectVesselButton" x:Name="selectVesselButton"/>
</Grid>