【发布时间】:2009-09-17 19:06:46
【问题描述】:
我有一个地址对象,简单定义如下:
public class Address
{
public string StreetNumber { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
}
相当简单。根据我对另一个question 的回答的建议,我指的是this 博客文章,将我的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);
一切都无济于事。
我有什么遗漏吗?
【问题讨论】:
标签: c# winforms validation data-binding idataerrorinfo