在 DevExpress LookupEdit 上配置或禁用验证?

Configure or disable validate on DevExpress LookupEdit?

我在 WinForms 应用程序上有一个 LookupEdit; EditValue 绑定到一个模型,其中 属性 是一个 int? 并标有应用的 Required 属性。我遇到的主要问题是 DX 控件检测到该属性,这没问题,但会阻止控件失去焦点,直到从列表中选择一个值。一旦错误提供程序显示,您甚至无法关闭应用程序。

如果您 运行 应用程序并尝试单击文本框,LookupEdit 会验证并防止焦点离开控件。理想情况下,我只想让焦点改变并显示错误提供者。我怎样才能 1) 配置 LookupEdit 让它失去焦点,即使控件无效也可以配置错误图标显示的位置或 2) 告诉 DX 忽略数据注释?如果模型无效,我们的表单代码将 enable/disable 一个保存按钮,我不需要 DX 强制用户保留在控件上直到它有效。

使用 .Net 4.7.2 创建名为 DxDataAnnotations 的 Windows Forms 项目,并使用版本 19.1.6 添加 devexpress.win NuGet 包。这是表单代码(包括模型):

Form1.cs

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Forms;

namespace DxDataAnnotations {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e) {
            base.OnShown(e);

            managersBindingSource.DataSource = new BindingList<Manager>(new[] { 
                new Manager(1, "Manager 1"),
                new Manager(2, "Manager 2"),
                new Manager(3, "Manager 3"),
                new Manager(4, "Manager 4"),
                new Manager(5, "Manager 5"),
                new Manager(6, "Manager 6")
            });

            employeeBindingSource.DataSource = new Employee();
        }
    }

    public sealed class Employee {
        [Required]
        public int? ManagerId { get; set; }
    }

    public sealed class Manager {
        public Manager(int id, string name) {
            ManagerId = id;
            ManagerName = name;
        }

        public int ManagerId { get; }
        public string ManagerName { get; }
    }
}

Form1.Designer.cs

namespace DxDataAnnotations {
    partial class Form1 {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            this.lookUpEdit1 = new DevExpress.XtraEditors.LookUpEdit();
            this.employeeBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.managersBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.textBox1 = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.lookUpEdit1.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.employeeBindingSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.managersBindingSource)).BeginInit();
            this.SuspendLayout();
            // 
            // lookUpEdit1
            // 
            this.lookUpEdit1.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.employeeBindingSource, "ManagerId", true));
            this.lookUpEdit1.Location = new System.Drawing.Point(207, 93);
            this.lookUpEdit1.Name = "lookUpEdit1";
            this.lookUpEdit1.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.lookUpEdit1.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("ManagerId", "Manager Id", 78, DevExpress.Utils.FormatType.Numeric, "", false, DevExpress.Utils.HorzAlignment.Far, DevExpress.Data.ColumnSortOrder.None, DevExpress.Utils.DefaultBoolean.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo("ManagerName", "Manager Name", 82, DevExpress.Utils.FormatType.None, "", true, DevExpress.Utils.HorzAlignment.Near, DevExpress.Data.ColumnSortOrder.None, DevExpress.Utils.DefaultBoolean.Default)});
            this.lookUpEdit1.Properties.DataSource = this.managersBindingSource;
            this.lookUpEdit1.Properties.DisplayMember = "ManagerName";
            this.lookUpEdit1.Properties.ValueMember = "ManagerId";
            this.lookUpEdit1.Size = new System.Drawing.Size(398, 20);
            this.lookUpEdit1.TabIndex = 0;
            // 
            // employeeBindingSource
            // 
            this.employeeBindingSource.AllowNew = false;
            this.employeeBindingSource.DataSource = typeof(DxDataAnnotations.Employee);
            // 
            // managersBindingSource
            // 
            this.managersBindingSource.DataSource = typeof(DxDataAnnotations.Manager);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(222, 135);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.lookUpEdit1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.lookUpEdit1.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.employeeBindingSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.managersBindingSource)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private DevExpress.XtraEditors.LookUpEdit lookUpEdit1;
        private System.Windows.Forms.BindingSource employeeBindingSource;
        private System.Windows.Forms.BindingSource managersBindingSource;
        private System.Windows.Forms.TextBox textBox1;
    }
}

如何从模型中删除验证属性(直接或通过将模型实例包装到特定的 DTO 中)?
无论如何,这里有一些答案:

Ideally I'd like to just let the focus change and show the error provider.

查看 Form.AutoValidate 属性,它管理控件在失去用户输入焦点时如何验证其数据。尽管有验证结果,AutoValidate.EnableAllowFocusChange 值允许用户在编辑器之间移动焦点。

...tell DX to ignore data annotations?

您可以使用 DevExpress.Data.Utils.AnnotationAttributes.AllowValidation 静态选项避免验证:

DevExpress.Data.Utils.AnnotationAttributes.AllowValidation = DevExpress.Utils.DefaultBoolean.False;