using CommunityToolkit.Mvvm.ComponentModel; using InfineonHMI.Common; using System.ComponentModel.DataAnnotations; using TwinCAT.TypeSystem; namespace Common; public sealed class InRangeAttribute(string propMin, string propMax) : ValidationAttribute { public string PropMin { get; } = propMin; public string PropMax { get; } = propMax; protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { object instance = validationContext.ObjectInstance; int minValue = (int)(instance.GetType().GetProperty(PropMin)?.GetValue(instance) ?? 1); int maxValue = (int)(instance.GetType().GetProperty(PropMax)?.GetValue(instance) ?? 1); int tempValue = (int)(value ?? 1); if (tempValue <= maxValue && tempValue >= minValue) return ValidationResult.Success; return new($"Value has to be greater than {minValue} and smaller then {maxValue}"); } } public sealed partial class ParamControlIntRangeVm : ObservableValidator, IDisposable, IChangeTrackingEx { private int initValue; [ObservableProperty] private int iMin; [ObservableProperty] private int iMax; [ObservableProperty] private string sName; private int value; [InRangeAttribute(nameof(IMin), nameof(IMax))] public int Value { get => this.value; set { SetProperty(ref this.value, value, true); if (value >= IMin && value <= IMax) { } } } public ParamControlIntRangeVm() { SName = "No Name:"; Value = 0; initValue = Value; } public void Dispose() { } public void AcceptChanges() { initValue = Value; } public bool IsChanged => initValue != Value; public void DiscardChanges() { Value = initValue; } }