You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
885 B

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace BindingValidationRuleSample.Validation
{
public class AgeRangeException : ValidationRule
{
public int MinAge { get; set; } = 0;
public int MaxAge { get; set; } = 150;
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
int age = 0;
if (!int.TryParse(value.ToString(), out age))
return new ValidationResult(false, "Wrong value.");
if (age >= this.MinAge && age <= this.MaxAge)
return new ValidationResult(true, null);
else
return new ValidationResult(false, $"Valid age range is {this.MinAge}~{this.MaxAge}");
}
}
}