C#-Windows Phone 8 TextBlock

如果没有文本,如何在Windows Phone 8中隐藏TextBlock?StackPanelTextBlock Text={Binding Name} FontSize=22 Margin=0,5,10,0 TextWrapping=NoWrap TextAlignment=Center TextTrimming=WordEllipsis...

如果没有文本,如何在Windows Phone 8中隐藏TextBlock?

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

如果文本块中没有任何文本,我想隐藏它.这怎么可能?

它是一个多个“ viewmodel”文本块,因此,由于每5-15秒加载20个以上的性能问题,它没有名称且无法单独检查.

解决方法:

您将需要创建一个IValueConverter来分析字符串的长度,

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

将转换器的实例添加到页面资源中,然后使用该转换器将Visibility属性绑定到描述…

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

本文标题为:C#-Windows Phone 8 TextBlock

基础教程推荐