add extension for adding enum values
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
using Telerik.WinControls.UI;
|
||||||
|
|
||||||
|
namespace Pilz.UI.Telerik.Extensions;
|
||||||
|
|
||||||
|
public static class RadListDataItemCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IEnumerable<RadListDataItem> AddEnumValues<T>(this RadListDataItemCollection @this) where T : struct, Enum
|
||||||
|
{
|
||||||
|
return AddEnumValues<T>(@this, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<RadListDataItem> AddEnumValues<T>(this RadListDataItemCollection @this, bool clearCollection) where T : struct, Enum
|
||||||
|
{
|
||||||
|
return AddEnumValues<T>(@this, clearCollection, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<RadListDataItem> AddEnumValues<T>(this RadListDataItemCollection @this, bool clearCollection, Func<T, bool>? filter, Func<T, string?>? format) where T : struct, Enum
|
||||||
|
{
|
||||||
|
var values = Enum.GetValues<T>();
|
||||||
|
var items = new List<RadListDataItem>();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user