添加了数据库相关了类,和枚举类型相关的类,并且将枚举类型绑定到前段

This commit is contained in:
2025-06-20 18:53:29 +08:00
parent 5dfce624c4
commit 908dc60439
29 changed files with 437 additions and 151 deletions

View 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;
}
}

View File

@@ -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);
}
}
}
}