Files
ZuneModdingHelper/OwlCore.Wpf/Converters/Bools/CollectionAnyToBoolConverter.cs
T

42 lines
1.4 KiB
C#
Raw Normal View History

2021-08-05 20:45:26 -05:00
using System;
using System.Collections;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Windows.Data;
using System.Globalization;
namespace OwlCore.Wpf.Converters.Bools
{
/// <summary>
/// A converter that converts a given <see cref="ICollection"/> to an bool based on the presence of any items in the <see cref="ICollection"/>.
/// </summary>
public sealed class CollectionAnyToBoolConverter : IValueConverter
{
/// <summary>
/// Gets whether or not a collection is not empty.
/// </summary>
/// <param name="collection">The collection to check.</param>
/// <returns>Whether or not the colleciton contains any elements</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Convert(ICollection collection) => collection.Count > 0;
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is ICollection collection)
{
return Convert(collection);
}
return false;
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}