3

How can I remove the row Comment is on if its null or empty string?

<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
  • Can you not just filter your list before binding? Commented Feb 6, 2018 at 17:33
  • Post a formatted code is cleaner, easier to read and respectful with community =) Commented Feb 6, 2018 at 17:57

2 Answers 2

7

This is the appropriate case to use a ValueConverter.

All you need to do is create a converter like this:

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();
        }
    }
}

And use it on the IsVisible property of Label:

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

Thus you can reuse it wherever you need.

Sign up to request clarification or add additional context in comments.

Comments

4

Your model:

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.