init
This commit is contained in:
101
Common/DTS.Common.Utilities/EnumDropDown.cs
Normal file
101
Common/DTS.Common.Utilities/EnumDropDown.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DTS.Common.Utilities
|
||||
{
|
||||
public class EnumDropDown<T> : System.Windows.Forms.ComboBox where T : struct, IComparable
|
||||
{
|
||||
/// <summary>
|
||||
/// This is the class which actually holds the entrys in the list
|
||||
/// </summary>
|
||||
public class EnumListEntry : IComparable<EnumListEntry>
|
||||
{
|
||||
public T Value { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var decoder = new DescriptionAttributeCoder<T>();
|
||||
return decoder.DecodeAttributeValue(Value);
|
||||
}
|
||||
|
||||
public EnumListEntry(T val)
|
||||
{
|
||||
Value = val;
|
||||
}
|
||||
|
||||
public int CompareTo(EnumListEntry other)
|
||||
{
|
||||
return Value.CompareTo(other.Value);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null || GetType() != obj.GetType())
|
||||
return false;
|
||||
var ele = (EnumListEntry)obj;
|
||||
return Value.Equals(ele.Value);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Value.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public delegate bool Filter(T enumEntry);
|
||||
|
||||
protected Filter _EnumFilter;
|
||||
public Filter EnumFilter
|
||||
{
|
||||
set
|
||||
{
|
||||
_EnumFilter = value;
|
||||
PopulateList();
|
||||
}
|
||||
}
|
||||
|
||||
public EnumDropDown(Filter defaultFilter)
|
||||
: base()
|
||||
{
|
||||
_EnumFilter = defaultFilter;
|
||||
PopulateList();
|
||||
}
|
||||
|
||||
public void PopulateList()
|
||||
{
|
||||
var values = Enum.GetValues(typeof(T));
|
||||
var entryList = new List<EnumListEntry>();
|
||||
foreach (T i in values)
|
||||
{
|
||||
if (_EnumFilter == null || _EnumFilter(i))
|
||||
{
|
||||
entryList.Add(new EnumListEntry(i));
|
||||
}
|
||||
}
|
||||
Items.Clear();
|
||||
Items.AddRange(entryList.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get or Set the selection as an EnumListEntry (can be null).
|
||||
/// </summary>
|
||||
public EnumListEntry SelectedEntry
|
||||
{
|
||||
get => (EnumListEntry)SelectedItem;
|
||||
|
||||
set => SelectedItem = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get or Set the selection as the enum itself (can NOT be null)!
|
||||
/// </summary>
|
||||
public T? SelectedEnum
|
||||
{
|
||||
get => SelectedEntry?.Value;
|
||||
|
||||
set => SelectedEntry = value == null ? null : new EnumListEntry((T)value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user