From 52c76bb58fb17c476a36aea1dff8116ad1c4efb4 Mon Sep 17 00:00:00 2001 From: TTLC198 <41226242+TTLC198@users.noreply.github.com> Date: Sat, 9 Sep 2023 16:53:37 +0300 Subject: [PATCH] Fixed a class that checks the length of a custom processor or video card name --- .../ValidationRules/HardwareNameLengthRule.cs | 15 +++++----- .../HardwareNameLengthRuleValue.cs | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 HSMonitor/Utils/ValidationRules/HardwareNameLengthRuleValue.cs 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