diff --git a/Pilz.UI.Telerik/Extensions/RadListDataItemCollectionExtensions.cs b/Pilz.UI.Telerik/Extensions/RadListDataItemCollectionExtensions.cs new file mode 100644 index 0000000..8e56bf5 --- /dev/null +++ b/Pilz.UI.Telerik/Extensions/RadListDataItemCollectionExtensions.cs @@ -0,0 +1,37 @@ +using Telerik.WinControls.UI; + +namespace Pilz.UI.Telerik.Extensions; + +public static class RadListDataItemCollectionExtensions +{ + public static IEnumerable AddEnumValues(this RadListDataItemCollection @this) where T : struct, Enum + { + return AddEnumValues(@this, false); + } + + public static IEnumerable AddEnumValues(this RadListDataItemCollection @this, bool clearCollection) where T : struct, Enum + { + return AddEnumValues(@this, clearCollection, null, null); + } + + public static IEnumerable AddEnumValues(this RadListDataItemCollection @this, bool clearCollection, Func? filter, Func? format) where T : struct, Enum + { + var values = Enum.GetValues(); + var items = new List(); + + format ??= v => Enum.GetName(v); + + if (clearCollection) + @this.Clear(); + + foreach (var value in values) + { + if (filter is null || filter(value)) + items.Add(new(format(value), value)); + } + + @this.AddRange(items); + + return items; + } +}