EmptyListToVisibilityConverter

前端 未结 6 1895
天涯浪人
天涯浪人 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:28

    I guess the problem is, that the collection object stays the same, when you add or remove items from it. So the binding does not update the value and the converter is not called.

    0 讨论(0)
  • 2021-01-19 03:32

    C# version 7 supports pattern matching. Now you can write:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch (value)
        {
            case null:
                return Visibility.Collapsed;
            case ICollection list:
                return list.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
        }
    
        return Visibility.Visible;
    }
    
    0 讨论(0)
  • 2021-01-19 03:39

    A better alternative is to use ObservableCollection and then bind to its Count property: WPF Converters and ObservableCollections

    0 讨论(0)
  • 2021-01-19 03:48

    My guess is that it's because of the fact that you're using IList<Object> in the converter but your actual collection is an IList<SomethingElse>. See, IList is not covariant so you can't just convert any IList<T> to an IList<Object>. My suggestion will be to try and use IEnumerable<object> in the converter instead and use the Count() extension method to determine the number of items in the collection.

    0 讨论(0)
  • 2021-01-19 03:50

    You can't cast your list to IList <Object>, but you can cast it to ICollection, and then use ICollection.Count : see http://devw.wordpress.com/2011/07/18/empty-list-visibility-converter/

    public class EmptyListVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;
            else
            {
                ICollection list = value as ICollection;
                if (list != null)
                {
                    if (list.Count == 0)
                        return Visibility.Collapsed;
                    else
                        return Visibility.Visible;
                }
                else
                    return Visibility.Visible;
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    
        {
            throw new NotImplementedException();
        }
    }
    <ListBox x:Name=”NameChoiceListBox”
             ItemsSource=”{Binding NamesList}”
             SelectedItem=”{Binding SelectedName, Mode=TwoWay}”
             ItemTemplate=”{StaticResource DataTemplateNameChoice}”
             Visibility=”{Binding NamesList, Converter={StaticResource EmptyListVisibilityConverter}}”>
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题