我有一个简单定义如下的地址对象:
public class Address
{
public string StreetNumber { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
相当简单。根据我提出的另一个问题的答案的建议,当我将我的 UI 数据绑定到 Person 类型的对象(其中包含 Address MailingAddress 字段)时,我指的是这篇博文。
问题是 IDataError 接口方法没有验证 Address 类型的任何属性。
public string this[string columnName]
{
get
{
string result = null;
// the following works fine
if(columnName == "FirstName")
{
if (string.IsNullOrEmpty(this.FirstName))
result = "First name cannot be blank.";
}
// the following does not run
// mostly because I don't know what the columnName should be
else if (columnName == "NotSureWhatToPutHere")
{
if (!Util.IsValidPostalCode(this.MailingAddress.PostalCode))
result = "Postal code is not in a know format.";
}
return result;
}
}
所以,显然我不知道 columnName 将是什么......我已经完成了它,除了任何公共属性(内在类型)之外,它从来没有其他任何东西。我什至尝试过运行和打破如下声明:
if (columnName.Contains("Mailing") || columnName.Contains("Postal"))
System.Windows.Forms.MessageBox.Show(columnName);
一切都无济于事。
有什么我想念的吗?