Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 723
Eric Avatar asked Dec 05 '25 05:12

Eric


1 Answers

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" />
like image 126
ZombieSheep Avatar answered Dec 07 '25 00:12

ZombieSheep