RowDefinition在ListView中使用Grid上的绑定属性时,高度不起作用? [英] RowDefinition Height not working when using bound property on Grid in ListView?

查看:142
本文介绍了RowDefinition在ListView中使用Grid上的绑定属性时,高度不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试设置Xamarin.Forms上的ListView中的Grid Row的高度时,会发生奇怪的行为。当我硬编码该值时,例如:

An odd behavior is occurring when trying to set the Height of a Grid Row that is inside a ListView on Xamarin.Forms. When I hard code the value, for example:

<RowDefinition Height="40" />

...行的高度设置为40.我知道,因为我尝试过不同值和高度按比例更改。

... the Height of the Row gets set to 40. I know because I've tried different values and the Height changed proportionally.

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

However when I set the same property by binding it to a property on the View Model, no matter what the value of the property is, the Height of the Row always remains the same.

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

这是我视图上的XAML代码:

Here is the XAML code on my View:

<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外的图像来测试视图模型上的属性,作品。有没有人知道为什么会发生什么,怎么解决?

I already tested the property on the view model by binding the height to an image outside the ListView and it works. Does anybody know why thins happens and how to fix it?

推荐答案

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

This is how I solved it if anyone else has the same issue. Any item inside the listview is bound to the ItemsSource objects. To solve the problem it is necessary to bind the height by referencing the Root of the ViewModel (BindingContext)

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

这篇关于RowDefinition在ListView中使用Grid上的绑定属性时,高度不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆