EmptyListToVisibilityConverter

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 if the passed object is null ) it should return Visibility.Collapsed; if the list is not empty it should return Visibility.Visibile;

I plan to use this for a datagrid. The plan is to make the datagrid invisible (collapsed) whenever the list given to the ItemsSource is an empty list or a null.

<my:DataGrid 
                    Name="dataGridAuxiliaryTools"
                    Style="{StaticResource DataGridStyle}"
                    CellStyle="{StaticResource DataGridCellStyle}"
                    ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}"
                    ItemsSource="{Binding Path=Items}"
                    IsReadOnly="False"
                    Visibility="{Binding Path=Items, 
                    Converter={StaticResource emptyListToVisibilityConverter}}"
 </my:DataGrid>

I wrote the EmptyListToVisibilityConverter as follows:

public class EmptyListToVisibilityConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter,
                          CultureInfo culture)
    {
        if (value == null)
        {
            return Visibility.Collapsed;
        }
        else if (value is IList<Object>)
        {
            if ((value as IList<Object>).Count == 0)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }
        else
        {
            return Visibility.Visible;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
        throw new NotImplementedException();
    }

This works well when Items is given as null but when Items is given as a List it does not work ... I think that the code bellow is not correct and cannot detect if "value" is a list or not ... Any hints ?

if (value is IList<Object>)
            {
                if ((value as IList<Object>).Count == 0)
                {
                    return Visibility.Collapsed;
                }
                else
                {
                    return Visibility.Visible;
                }

Any hints on how to do this ?

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

wpf - EmptyListToVisibilityConverter, You can't cast your list to IList <Object>, but you can cast it to ICollection, and then use ICollection.Count : see  Your setter doesn't use value so I presume you are never setting the value of ShowAllUsersEmptyLabel. The answer I think you are looking for is binding the label IsVisible property to AllUsers and then use a property converter to check if it is empty or not.

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.

Billionaires-Universal/EmptyListToVisibilityConverter.cs at master , UI.Xaml;. using Windows.UI.Xaml.Data;. namespace Billionaires.Universal.Mvvm.​Converters. {. public class EmptyListToVisibilityConverter : IValueConverter. Commonly used converters that allow the data to be modified as it passes through the binding engine. BoolNegationConverter. Converts a boolean to the inverse value (True to False and vice versa) BoolToObjectConverter. Converts a boolean value into an object. The converted value is selected between the values of TrueValue and FalseValue properties.

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

wpf - EmptyListToVisibilityConverter, public class EmptyListToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if  Contribute to saguiitay/Billionaires-Universal development by creating an account on GitHub.

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.

empty listbox message wp7 - windows-phone-7, EmptyListToVisibilityConverter. 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 )  Data binding overview. 10/05/2018; 15 minutes to read +4; In this article. This topic shows you how to bind a control (or other UI element) to a single item or bind an items control to a collection of items in a Universal Windows Platform (UWP) app.

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

Package de converters pour les applications Windows Store , EmptyListToVisibilityConverter : Permet d'afficher une liste que si elle contient des items; EqualsToBoolConverter : Compare des valeurs avec  ListView is one of my favourite UI controls available in Xamarin.Forms. It is mostly easy to use and customise. Just bind a collection of data, define the representation of each item and you are done!

wpf - EmptyListToVisibilityConverter, Benim tahminim kullandığınız olduğu gerçeği nedeniyle olmasıdır IList<Object> dönüştürücü ama gerçek koleksiyonu bir olduğunu IList<SomethingElse> . Don't Write WPF Converters; Write C# Inline In Your XAML Instead Using QuickConverter 5 minute read December 13, 2013 If you’ve used binding at all in WPF then you more then likely have also written a converter. There are lots of tutorials on creating converters, so I’m not going to discuss that in length here.

ModernUI + Prism.DryIoc, v0.9.2.0 Touch support fixes, changes and support for more controls. MonoGame works fine, SunBurn still have some issues. Virtual Keyboard control - new control for touch devices, auto open on TextBox control

Comments
  • Hi MadSeb. Please stop signing your posts. Cordially, and with best regards to you and your children,
  • I used "Any" rather than Count()>0 > ((IEnumerable<object>) value).Any()? Visibility.Visible: Visibility.Collapsed;