重构项目,将项目拆分(临时提交)
This commit is contained in:
52
DMS.WPF/Extensions/EnumBindingSourceExtension.cs
Normal file
52
DMS.WPF/Extensions/EnumBindingSourceExtension.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace DMS.Extensions;
|
||||
|
||||
internal class EnumBindingSourceExtension : MarkupExtension
|
||||
{
|
||||
private Type? _enumType;
|
||||
|
||||
public EnumBindingSourceExtension()
|
||||
{
|
||||
}
|
||||
|
||||
public EnumBindingSourceExtension(Type enumType)
|
||||
{
|
||||
EnumType = enumType;
|
||||
}
|
||||
|
||||
public Type? EnumType
|
||||
{
|
||||
get => _enumType;
|
||||
set
|
||||
{
|
||||
if (value != _enumType)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
var enumType = Nullable.GetUnderlyingType(value) ?? value;
|
||||
if (!enumType.IsEnum)
|
||||
throw new ArgumentException("Type must be for an Enum.");
|
||||
}
|
||||
|
||||
_enumType = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (_enumType == null)
|
||||
throw new InvalidOperationException("The EnumType must be specified.");
|
||||
|
||||
var actualEnumType = Nullable.GetUnderlyingType(_enumType) ?? _enumType;
|
||||
var enumValues = Enum.GetValues(actualEnumType);
|
||||
|
||||
if (actualEnumType == _enumType)
|
||||
return enumValues;
|
||||
|
||||
var tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
|
||||
enumValues.CopyTo(tempArray, 1);
|
||||
return tempArray;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user