EmptyListToVisibilityConverter

前端 未结 6 1954
天涯浪人
天涯浪人 2021-01-19 03:21

I\'m trying to do an \"empty list to visibility converter\" for WPF. This is an IValueConverter that takes an object ( that should be a list ) and if the list is empty (or i

6条回答
  •  爱一瞬间的悲伤
    2021-01-19 03:54

    I think its quite straight forward, here you go:

      public class NullOrEmptyCollectionToVisibilityConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
          if (value == null) return Visibility.Collapsed;
    
          var collection = value as ICollection;
          return collection != null ? (collection.Count == 0 ? Visibility.Collapsed : Visibility.Visible) : Visibility.Visible;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    

    Hoping wil help, thanks! - Shams

提交回复
热议问题