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

标签 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>

最佳答案

这是使用 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 的 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>

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

关于xaml - 如果 ListView 中的数据为空,则隐藏行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48648585/

相关文章:

c# - 为什么这种简单的风格不适用?

wpf - WPF 的 TextBox 是否支持荷兰的拼写检查词典?

c# - 通过单击背景在 ListBox 中选择一个 CheckBox

xamarin - 'Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.TrackingExpressionNode' 的类型初始值设定项引发异常

c# - UWP:获取当前 CultureInfo

Xamarin 形式 : Content View doesn't display in content page

WPF:如何设置内容控制的数据模板触发器?

xamarin - 在 Xamarin.Android 中,需要通过使用 DependencyService 从 Xamarin.Forms 调用来使用 Activity

c# - "FreshIOC.Container.Register"有什么作用?

c# - TreeView 上下文菜单在 HierarchicalDataTemplate 中不起作用