Xamarin Forms ListView隐藏单元格中的空白行

时间:2017-01-12 20:52:08

标签: xaml xamarin xamarin.forms

我在ListView中为自定义单元格提供了以下XAML(简化为一个简单示例)。

<ListView x:Name="___drives" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding Address}" FontAttributes="Bold" />
                    <Label Text="{Binding Scheduled}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

在代码隐藏中,我将Drive对象的ObervableCollection绑定到ListView。 Drive对象定义如下。它扩展了一个实现INotifyPropertyChanged接口的ModelBase(详细信息未显示,但是是样板文件)。

public class Drive : ModelBase
{
    private string _address;
    private string _scheduled;

    public string Address
    {
        get { return _address; }
        set { if (_address != value) { _address = value; OnPropertyChanged("Address"); } }
    }
    public string Scheduled
    {
        get { return _scheduled; }
        set { if (_scheduled != value) { _scheduled = value; OnPropertyChanged("Scheduled"); } }
    }
}

有时Scheduled为null。当值为null时,有没有办法让ViewCell不为Scheduled属性显示空行?

下面的图片是Scheduled没有值时产生的结果(包含更多字段,但您明白了)。我画了红框以显示空白行。我想让DistanceToMe向上移动并占据空白行的空间。

enter image description here

1 个答案:

答案 0 :(得分:1)

如果可用,您可以在Label上使用IsVisible属性。您可以在ViewModel上拥有一个布尔属性,当Schedualed为null时,该属性设置为false。 For example

相关问题