How to hide the empty TextBlock?

前端 未结 3 829
迷失自我
迷失自我 2020-12-08 00:10

In the XAML provided below, I don\'t have the value for Phone sometimes. When that happens, the value is missing, but the TextBlock is still occupying the space

3条回答
  •  余生分开走
    2020-12-08 00:55

    Instead of introducing a style just for this, for this sort of thing I tend to prefer using a converter that will handle a null or empty string.

    
    

    Where StringToVisibilityConverter is defined like this:

    [ValueConversion(typeof(string), typeof(Visibility))]
    public class StringToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (string.IsNullOrEmpty((string)value))
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

提交回复
热议问题