更多验证条件

我有3个文本框,当它不满足特定条件时,我尝试添加验证提示。条件如下:

Disable submit button and show hints until textbox1 <= textbox2 and textbox3 >= 15% of textbox2

现在,我只能通过以下方法弄清楚如何检查长度或将其设为必需:

public class AttributeValidationViewModel : AnnotationValidationViewModel
    {
        private string _FirstName;
        [Required(ErrorMessage = "# of containers is required")]
        [MinLength(3,ErrorMessage = "textbox must have at least three characters")]


        public string FirstName
        {
            get => _FirstName;
            set => Set(ref _FirstName,value);
        }

        private string _LastName;
        [Required]
        public string LastName
        {
            get => _LastName;
            set => Set(ref _LastName,value);
        }

        public RelayCommand SubmitCommand { get; }

        public AttributeValidationViewModel()
        {
            SubmitCommand = new RelayCommand(OnSubmit,CanSubmit);
            //Doing this will cause the errors to show immediately
            ValidateModel();
        }

        private bool CanSubmit()
        {
            return !HasErrors;
        }

        private void OnSubmit()
        {
            Debug.WriteLine("Form Submittedffff");
        }

我想添加类似的内容

[CustomCondition(ErrorMessage = "textbox must have at least three characters")] (like line 5 of the code)

我只是不知道该怎么做。我知道如何在有条件的情况下禁用该按钮,但是我也在尝试在文本框中提供验证提示。

pandadizi 回答:更多验证条件

数据注释不是执行一次涉及多个属性的验证的方法。

您应该做的是实现.NET Framework 4.5中引入的INotifyDataErrorInfo接口。请参阅this TechNet article,以获取更多信息和实现方法的示例。

本文链接:https://www.f2er.com/3109569.html

大家都在问