using System; using System.Collections.Generic; using System.Linq; using MPF.Core.Converters; namespace MPF.Core.UI.ComboBoxItems { /// /// A generic combo box element /// /// Enum type representing the possible values public class Element : IEquatable>, IElement where T : struct, Enum { private readonly T Data; public Element(T data) => Data = data; /// /// Allow elements to be used as their internal enum type /// /// public static implicit operator T? (Element item) => item?.Data; /// public string Name => EnumConverter.GetLongName(Data); public override string ToString() => Name; /// /// Internal enum value /// public T Value => Data; /// /// Determine if the item is selected or not /// /// Only applies to CheckBox type public bool IsChecked { get; set; } /// /// Generate all elements associated with the data enum type /// /// public static IEnumerable> GenerateElements() { return Enum.GetValues(typeof(T)) .OfType() .Select(e => new Element(e)); } /// public override bool Equals(object? obj) { return Equals(obj as Element); } /// public bool Equals(Element? other) { if (other == null) return false; return Name == other.Name; } /// public override int GetHashCode() => base.GetHashCode(); } }