WPF - 使用 Fody Validar 进行用户输入验证

WPF - User input validation using Fody Validar

我正在尝试验证上图中的用户输入。 我正在使用 Fody Validor 库并按照说明 here.

我可以看到正在进行验证,但文本框边框没有变成红色。

我用谷歌搜索过,但无法获得这方面的信息。 谁能给我提供一个工作代码示例?

更新

Fody ValidationTemplate(数据注解验证)

public class ValidationTemplate :IDataErrorInfo
    {

        INotifyPropertyChanged target;
        ValidationContext validationContext;
        List<ValidationResult> validationResults;

        public ValidationTemplate(INotifyPropertyChanged target)
        {
            this.target = target;
            validationContext = new ValidationContext(target, null, null);
            validationResults = new List<ValidationResult>();
            Validator.TryValidateObject(target, validationContext, validationResults, true);
            target.PropertyChanged += Validate;
        }

        void Validate(object sender, PropertyChangedEventArgs e)
        {
            validationResults.Clear();
            Validator.TryValidateObject(target, validationContext, validationResults, true);
            var hashSet = new HashSet<string>(validationResults.SelectMany(x => x.MemberNames));
            foreach (var error in hashSet)
            {
                OnValidate(error);
            }
        }


        private HashSet<string> _errors = new HashSet<string>();
        public string this[string columnName]
        {
            get
            {
                return OnValidate(columnName);
            }
        }

        protected virtual string OnValidate(string propertyName)
        {
            string error = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(propertyName))
                    throw new ArgumentException("Invalid property name", propertyName);

                var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
                var results = new List<ValidationResult>(1);
                var context = new ValidationContext(this, null, null) { MemberName = propertyName };
                var result = Validator.TryValidateProperty(value, context, results);
                if (!result)
                {
                    var validationResult = results.First();
                    error = validationResult.ErrorMessage;
                    _errors.Add(propertyName);
                }
                else
                {
                    _errors.Remove(propertyName);
                }
            }
            catch (Exception) { }

            return error;
        }

        public bool HasErrors
        {
            get { return _errors.Count > 0; }
        }

        public string Error { get; set; }
    }

要验证的模型 class:

public class Movie :  INotifyPropertyChanged
    {
        IDataErrorInfo validationTemplate;
        public Movie()
        {
            validationTemplate = new ValidationTemplate(this);
        }

        private int _id;
        private string _title;
        public int Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged("Id");
                }
            }
        }

        [Required]
        [StringLength(30)]
        [MinLength(3)]
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    OnPropertyChanged("Title");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

XAML

<TextBox Text="{Binding Movie.Title, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, NotifyOnValidationError=True, ValidatesOnNotifyDataErrors=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />

我已经调查并发现了这个问题。 FodyWeavers.xml 文件中缺少以下配置。

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <Validar/>
</Weavers>

当我更新 Fody nuget 包时删除了节点 <Validar/>