made EnumItemsSourceConverter in advance of AlgoEditorGridView (will be used by enum param DataTemplate)

This commit is contained in:
John Colvin committed 2019-03-28 11:52:32 -05:00
1 parent 81ed77d4c9
commit 544ddff37f
1 file changed
+41
@@ -0,0 +1,41 @@
using mbts.collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
namespace mbts.ui.converters {
class EnumItemsSourceConverter : IValueConverter {
readonly ObservableKeyedCollection<EnumItemsSourceParams, Type> _map = new ObservableKeyedCollection<EnumItemsSourceParams, Type>(p => p.EnumType);
public IList Map => _map;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if (value != null) {
var t = value.GetType();
if (t.IsEnum) {
if (_map.TryGetValue(t, out EnumItemsSourceParams p)) {
return p.filter_values(Enum.GetValues(t));
}
return Enum.GetValues(t);
}
}
return Enumerable.Empty<object>();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
}
class EnumItemsSourceParams {
public Type EnumType { get; set; }
public IList Except { get; set; } = new List<object>();
public IEnumerable filter_values(Array all) {
foreach (var v in all) {
if (!Except.Contains(v))
yield return v;
}
}
}
}