如果ListView中的数据为空,则隐藏行

时间:2018-02-06 17:21:48

标签: xaml xamarin.forms

如果或空字符串?

,则如何删除注释行
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30*" />
                    <ColumnDefinition Width="50*" />
                </Grid.ColumnDefinitions>
                <Label Text="{Binding EntryDate}" Grid.Column="0" Grid.Row="0"></Label>
                <Label Text="{Binding Sleep}" Grid.Column="1" Grid.Row="0"></Label>
                <Label Text="{Binding Comment}" Grid.Column="0" Grid.ColumnSpan="4" 
                       Grid.Row="1"></Label>
            </Grid>                    
        </ViewCell>                
    </DataTemplate>            
</ListView.ItemTemplate>

2 个答案:

答案 0 :(得分:5)

这是使用ValueConverter的合适案例。

您需要做的就是创建一个这样的转换器:

namespace App.Converters
{
    public class TextToBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value != null)
                if (!(value is string)) return true;
            return string.IsNullOrWhiteSpace(value as string) ? false : true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

并在Label:{/ p>的IsVisible属性上使用它

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converters="clr-namespace:App.Converters"
             x:Class="App.Views.SamplePage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <converters:TextToBoolConverter x:Key="TextToBoolConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30*" />
            <ColumnDefinition Width="50*" />
        </Grid.ColumnDefinitions>
        <Label Text="{Binding EntryDate}" Grid.Column="0" Grid.Row="0"></Label>
        <Label Text="{Binding Sleep}" Grid.Column="1" Grid.Row="0"></Label>
        <Label Text="{Binding Comment}" Grid.Column="0" Grid.ColumnSpan="4" 
               Grid.Row="1"
               IsVisible={Binding Comment, Converter={StaticResource TextToBoolConverter}}/>
    </Grid>           
</ContentPage>

因此,您可以在任何需要的地方重复使用它。

答案 1 :(得分:3)

你的模特:

public class MyModel
{

    //...other properties ...

    public string Comment { get; set; }

    public bool HasComment => !string.IsNullOrEmpty(Comment);
}



<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30*" />
                    <ColumnDefinition Width="50*" />
                </Grid.ColumnDefinitions>
                <Label Text="{Binding EntryDate}" Grid.Column="0" Grid.Row="0"></Label>
                <Label Text="{Binding Sleep}" Grid.Column="1" Grid.Row="0"></Label>

                <Label Text="{Binding Comment}" Grid.Column="0" Grid.ColumnSpan="4" 
                       Grid.Row="1" IsVisible = {Binding HasComment}></Label>
            </Grid>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>