【问题标题】:Xamarin Xaml Bind height of button to height of LabelXamarin Xaml 将按钮的高度绑定到标签的高度
【发布时间】:2019-03-26 23:17:19
【问题描述】:

我有一个标签和一个按钮在网格中彼此相邻。我正在尝试让按钮将其高度与标签匹配。

当前 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 属性绑定到标签的HeightHeightRequest 属性。但是,两者都不起作用。如何将按钮的高度绑定到标签的高度?

【问题讨论】:

  • 嘿,问题解决了吗?
  • 抱歉,我已经好几天没碰我的电脑了。您的解决方案暂时有效,但我宁愿将它绑定到标签的高度,而不是在标签本身上设置 HeightRequest(查看我的其他评论)。你知道这是否可能吗?

标签: xaml xamarin button label


【解决方案1】:

原因:

你好像忘记设置行高了。当网格只有一行时。行的默认高度将设置为屏幕的高度。

解决方案:

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

<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>

【讨论】:

  • 你好!回复晚了非常抱歉。上面显示的代码是大图的一部分,因此网格行的高度不是问题。我绑定高度的方式是绑定到标签的HeightRequest,当时没有设置。设置此属性(如您所做的那样)将按钮的高度请求绑定到标签的高度请求。但是,为了这个问题,您知道如何在不设置标签的HeightRequest的情况下实现这一点吗?
  • 你在哪里设置标签的高度?可以提供样品吗?
  • 我正在使用small 字体大小,否则我没有设置自定义高度。 CSS 文件中的所有 HeaderSub 都是 .headerSub { font-size: small; color: #BBBBBB; margin: 5, 0, 0, 0; }
猜你喜欢
  • 2017-02-21
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多