【问题标题】:RowDefinition Height not working when using bound property on Grid in ListView?在 ListView 中的 Grid 上使用绑定属性时,RowDefinition Height 不起作用?
【发布时间】:2017-06-01 00:19:35
【问题描述】:

尝试在 Xamarin.Forms 上的 ListView 内设置网格行的高度时发生了奇怪的行为。当我对值进行硬编码时,例如:

<RowDefinition Height="40" />

...行的高度设置为 40。我知道是因为我尝试了不同的值并且高度按比例改变。

但是,当我通过将其绑定到视图模型上的属性来设置相同的属性时,无论该属性的值是什么,行的高度始终保持不变。

<RowDefinition Height="{Binding SmallImageHeight}" />

这是我视图上的 XAML 代码:

<ListView ItemsSource="{Binding Items}" HasUnevenRows="True" x:Name="itemsListView">
      ...
              <StackLayout>
                <Grid>
                  <Grid.RowDefinitions>
                    <RowDefinition Height="{Binding SmallImageHeight}" />
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="5*" />
                    <ColumnDefinition Width="5*" />
                  </Grid.ColumnDefinitions>

                  <Image Source="{Binding SmallImagePath}" 
                         Aspect="AspectFill" 
                         Grid.Row="0" 
                         Grid.Column="0" />

                  <StackLayout Grid.Row="0" Grid.Column="1" >
                    ...
                  </StackLayout>
                </Grid>

              </StackLayout>
            ...
    </ListView>

我已经通过将高度绑定到 ListView 外部的图像来测试视图模型上的属性,并且它可以工作。有谁知道为什么会发生变薄以及如何解决它?

【问题讨论】:

  • 你试过VerticalAlignment="Stretch"的ListView
  • 其实SmallImageHeight 是有界的,高度和图像源一样
  • @MohitShrivastava 我应该什么时候设置 VerticalAlignment?我只是尝试将它设置在标题 ...r ...它崩溃了
  • @un-lucky SmalImageHeight 是一个浮点数。我修改了我的代码以将其发布在此处,并将该属性名称粘贴到图像源上。
  • 您的身高可能与行中的数据同时绑定。好吧,我不确定这一点。但是尝试通过不同的模型绑定高度(尝试先绑定它)和通过差异模型的数据(尝试稍后绑定)

标签: c# xaml xamarin data-binding xamarin.forms


【解决方案1】:

如果其他人有同样的问题,我就是这样解决的。列表视图中的任何项目都绑定到 ItemsSource 对象。为了解决这个问题,需要通过引用ViewModel的Root(BindingContext)来绑定高度

<RowDefinition Height="{Binding Source={x:Reference Name=itemsListView}, Path=BindingContext.SmallImageHeight}" /> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2018-03-15
    • 2016-05-04
    • 2020-11-26
    • 2011-03-01
    相关资源
    最近更新 更多