Skip to content

Instantly share code, notes, and snippets.

@emoacht
Created January 5, 2023 01:18
Show Gist options
  • Save emoacht/5dbe8bad43fdf67076ec28433f3cf4fe to your computer and use it in GitHub Desktop.
Save emoacht/5dbe8bad43fdf67076ec28433f3cf4fe to your computer and use it in GitHub Desktop.
Converts the state whether the source object is a collection and it has any item to boolean.
using System;
using System.Collections;
using System.Globalization;
using System.Windows.Data;
public class CollectionToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is IEnumerable collection)
{
foreach (var _ in collection)
{
return true;
}
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
@emoacht
Copy link
Author

emoacht commented Jan 5, 2023

This converter should work but in practice, there is almost no use because it will be called only when the source collection is set or replaced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment