diff --git a/HSMonitor/Utils/ValidationRules/HardwareNameLengthRule.cs b/HSMonitor/Utils/ValidationRules/HardwareNameLengthRule.cs index b89b368..8532de0 100644 --- a/HSMonitor/Utils/ValidationRules/HardwareNameLengthRule.cs +++ b/HSMonitor/Utils/ValidationRules/HardwareNameLengthRule.cs @@ -1,26 +1,27 @@ using System; using System.Globalization; +using System.Windows; using System.Windows.Controls; +using System.Windows.Markup; namespace HSMonitor.Utils.ValidationRules; +[ContentProperty("RuleValue")] public class HardwareNameLengthRule: ValidationRule { - public int Max { get; set; } - - public int Min { get; set; } + public HardwareNameLengthRuleValue RuleValue { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { try { - if (((string)value).Length > Max) + if (((string)value).Length > RuleValue.Max) return new ValidationResult(false, - $"Please enter a string less than {Max}."); + $"Please enter a string less than {RuleValue.Max}."); //todo localization - if (((string)value).Length < Min) + if (((string)value).Length < RuleValue.Min) return new ValidationResult(false, - $"Please enter a string more than {Min}."); + $"Please enter a string more than {RuleValue.Min}."); } catch (Exception e) { diff --git a/HSMonitor/Utils/ValidationRules/HardwareNameLengthRuleValue.cs b/HSMonitor/Utils/ValidationRules/HardwareNameLengthRuleValue.cs new file mode 100644 index 0000000..83bfef1 --- /dev/null +++ b/HSMonitor/Utils/ValidationRules/HardwareNameLengthRuleValue.cs @@ -0,0 +1,30 @@ +using System.Windows; + +namespace HSMonitor.Utils.ValidationRules; + +public class HardwareNameLengthRuleValue : DependencyObject +{ + public static readonly DependencyProperty MinBindingProperty = DependencyProperty.RegisterAttached( + nameof(Min), + typeof(int), + typeof(HardwareNameLengthRuleValue), + new PropertyMetadata(default(int)) + ); + + public static readonly DependencyProperty MaxBindingProperty = DependencyProperty.RegisterAttached( + nameof(Max), + typeof(int), + typeof(HardwareNameLengthRuleValue), + new PropertyMetadata(default(int)) + ); + + public int Min { + get => (int) GetValue(MinBindingProperty); + set => SetValue(MinBindingProperty, value); + } + + public int Max { + get => (int) GetValue(MaxBindingProperty); + set => SetValue(MaxBindingProperty, value); + } +} \ No newline at end of file