using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using Bentley.DgnPlatformNET; using Bentley.GeometryNET; using CommunityToolkit.Mvvm.ComponentModel; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace QuickModeling.Models { /// /// 项参数 /// public partial class Parameter : ObservableValidator, ICloneable { /// /// 对应CustomProperty的数据类型 /// [ObservableProperty] private TypeKind dataType; /// /// 对应ItemType /// [ObservableProperty] [NotifyPropertyChangedFor(nameof(FullName))] private string group; /// /// 对应CustomProperty的名称 /// [ObservableProperty] [Required(ErrorMessage = "属性名称必填")] [MinLength(1, ErrorMessage = "参数名称不能为空")] [MaxLength(20, ErrorMessage = "参数名称不能超过20个字符")] [NotifyDataErrorInfo] [NotifyPropertyChangedFor(nameof(FullName))] private string name; /// /// 对应CustomProperty的值 /// [ObservableProperty] [NotifyDataErrorInfo] [CustomValidation(typeof(Parameter), nameof(ValidateValue))] private object value; ///// ///// 工作单位 ///// //[ObservableProperty] //private WorkingUnit unit = WorkingUnit.Unspecified; /// /// 对应CustomProperty的单位 /// //[ObservableProperty] //private DgnECUnit dgnUnit; partial void OnDataTypeChanged(TypeKind kind) { if (string.IsNullOrEmpty(Value?.ToString())) { switch (kind) { case TypeKind.Boolean: Value = false; break; case TypeKind.Double: Value = 0.0; break; case TypeKind.Integer: Value = 0; break; case TypeKind.String: Value = string.Empty; break; } } } public object Clone() { var copy = (Parameter)MemberwiseClone(); //copy.DgnUnit = new DgnECUnit(Unit); return copy; } public static ValidationResult ValidateValue(string value, ValidationContext context) { if (string.IsNullOrEmpty(value)) { return new ValidationResult("参数值为空"); } Parameter instance = (Parameter)context.ObjectInstance; switch (instance.DataType) { case TypeKind.Boolean: if (bool.TryParse(value, out _)) { return ValidationResult.Success; } break; case TypeKind.Double: if (double.TryParse(value, out _)) { return ValidationResult.Success; } break; case TypeKind.Integer: if (int.TryParse(value, out _)) { return ValidationResult.Success; } break; case TypeKind.String: // 字符串类型不需要额外验证 return ValidationResult.Success; } return new("参数值与值类型不匹配"); } //public string Name { get => name; set => SetProperty(ref name, value, true); } public string FullName => $"{Group}-{Name}"; } }