2

我有一个绑定到 winform 的对象,这个对象实现了 IDataErrorInfo。我有一个错误提供程序。问题是当 aa 属性的属性发生变化时。

更改年龄时没有问题(即规则已正确检查并显示/删除)。但是当我更改职位时,错误不会显示/删除(实际上属性 Title 不属于对象人员)。我该如何进行检查?

this.errorProvider1.DataSource = this.bindingSourcePerson;
bindingSourcePerson.DataSource = new Person();
textBoxAge.DataBindings.Add("Text", bindingSourcePerson, "Age");
textBoxJobTitle.DataBindings.Add("Text", bindingSourcePerson, "CurrentJob.Title");

public class Person : IDataErrorInfo
{
    public double Age { get; set; }
    private Job _job = new Job();
    public Job CurrentJob { get { return _job; } set { _job = value; } }

    public string this[string columnName]
    {
        get
        {
            _lastError = "";
            switch (columnName)
            {
                case "Age":
                case "CurrentJob.Title":
                    if (!string.IsNullOrEmpty(CurrentJob.Title) && Age < 16)
                        _lastError = "Invalid job.";
                    break;

                default: _lastError = "";
                    break;

            }
            return _lastError;

        }
    }

    private string _lastError = "";
    public string Error
    {
        get { return _lastError; }
    }

    public class Job
    {
        public string Title { get; set; }
    }
4

1 回答 1

0

如果你向你的Person类添加一个属性:

public String CurrentJobTitle { get { return _job.Title; } }

然后绑定TextBoxJobTitleCurrentJobTitle

textBoxJobTitle.DataBindings.Add("Text", bindingSourcePerson, "CurrentJobTitle");

或者,像这样绑定TextBoxJobTitlebindingSourcePerson.CurrentJob

textBoxJobTitle.DataBindings.Add("Text", bindingSourcePerson.CurrentJob, "Title");

它会起作用吗?

于 2009-09-02T10:05:59.157 回答