RowDefinition在ListView中使用Grid上的绑定属性时,高度不起作用?

时间:2017-01-17 03:27:23

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

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

<RowDefinition Height="40" />

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

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

<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外部的图像来测试视图模型上的属性,并且它可以正常工作。有谁知道为什么会发生这种情况以及如何解决它?

1 个答案:

答案 0 :(得分:0)

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

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