C # IDataErrorInfo and child property
I have a winform related object and this object implements IDataErrorInfo. I have an error provider. The problem is changing the property of property a.
No problem when changing the age (i.e. rules are checked and displayed / removed correctly). But when I change the title of the job, the error is not displayed / removed (indeed, the Title property does not belong to the entity face). How can I complete a check?
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; }
}
source to share
if you add a property to the class Person
:
public String CurrentJobTitle { get { return _job.Title; } }
and then bind TextBoxJobTitle
to CurrentJobTitle
:
textBoxJobTitle.DataBindings.Add("Text", bindingSourcePerson, "CurrentJobTitle");
Or bind TextBoxJobTitle
to bindingSourcePerson.CurrentJob
like this:
textBoxJobTitle.DataBindings.Add("Text", bindingSourcePerson.CurrentJob, "Title");
will it work?
source to share