Eric
Eric

Reputation: 1268

Windows Phone 8 TextBlock

How do I hide a TextBlock in Windows Phone 8 if it has no text?

<StackPanel>
    <TextBlock Text="{Binding Name}" FontSize="22" Margin="0,5,10,0" TextWrapping="NoWrap" TextAlignment="Center" TextTrimming="WordEllipsis" />
    <Image Source="{Binding Icon}" MaxWidth="36" MaxHeight="36" HorizontalAlignment="Left" Margin="10,-33,10,10" Stretch="Fill"/>
    <TextBlock Text="{Binding Description}" FontSize="14" Margin="10,0,10,5" MaxHeight="60" TextWrapping="Wrap" TextTrimming="WordEllipsis" />
</StackPanel>

I would like to hide the textblock "description" if it doesn't have any text inside it. How would this be possible?

It's a multiple "viewmodel" textblock, therefore it has no name and can't be checked individually, due to performance issues of loading over 20+ every 5 - 15 seconds.

Upvotes: 3

Views: 1307

Answers (1)

ZombieSheep
ZombieSheep

Reputation: 29953

You will need to create an IValueConverter that analyses the length of the string,

public class HideEmptyStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var input = (string)value;
        return string.IsNullOrWhiteSpace(input) ? Visibility.Collapsed : Visibility.Visible;
    }

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

add an instance of the converter to your page's resources and then bind the Visibility property to the description using that converter...

<TextBlock Text="{Binding Description}" Visibility="{Binding Description, Converter={StaticResource HideEmptyStringConverter}}" FontSize="14" Margin="10,0,10,5" MaxHeight="60" TextWrapping="Wrap" TextTrimming="WordEllipsis" />

Upvotes: 3

Related Questions