添加了数据库相关了类,和枚举类型相关的类,并且将枚举类型绑定到前段
This commit is contained in:
50
Extensions/EnumBindingSourceExtension.cs
Normal file
50
Extensions/EnumBindingSourceExtension.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace PMSWPF.Extensions;
|
||||
|
||||
class EnumBindingSourceExtension : MarkupExtension
|
||||
{
|
||||
private Type? _enumType;
|
||||
|
||||
public Type? EnumType
|
||||
{
|
||||
get => _enumType;
|
||||
set
|
||||
{
|
||||
if (value != _enumType)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
|
||||
if (!enumType.IsEnum)
|
||||
throw new ArgumentException("Type must be for an Enum.");
|
||||
}
|
||||
|
||||
_enumType = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EnumBindingSourceExtension() { }
|
||||
|
||||
public EnumBindingSourceExtension(Type enumType)
|
||||
{
|
||||
EnumType = enumType;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,27 @@ public static class ObjectExtensions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个泛型对象,将source对象上的所有属性的值,都转换到新创建对象上
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static void NewTo<T>(this Object source ) where T : new()
|
||||
{
|
||||
T target = new T();
|
||||
var sourceType = source.GetType();
|
||||
var targetType = target.GetType();
|
||||
var sourceProperties = sourceType.GetProperties();
|
||||
foreach (PropertyInfo sourceProperty in sourceProperties)
|
||||
{
|
||||
PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name);
|
||||
if (targetProperty!= null && targetProperty.CanWrite && sourceProperty.CanRead && targetProperty.PropertyType == sourceProperty.PropertyType)
|
||||
{
|
||||
object value = sourceProperty.GetValue(source, null);
|
||||
targetProperty.SetValue(target, value, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user