gpt4 book ai didi

c# - 具有复杂类型的 IDataErrorInfo

转载 作者:行者123 更新时间:2023-11-30 22:48:51 30 4
gpt4 key购买 nike

我有一个简单定义如下的地址对象:

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 数据绑定(bind)到 Person 类型的对象(包含 Address MailingAddress 字段)时的博文。

问题是 IDataError 接口(interface)方法没有验证地址类型的任何属性。

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 是什么......我已经检查过它并且除了任何公共(public)属性(内部类型)之外从来没有任何其他东西。我什至尝试过运行并中断如下语句:

if (columnName.Contains("Mailing") || columnName.Contains("Postal"))
System.Windows.Forms.MessageBox.Show(columnName);

一切都无济于事。

有什么我想念的吗?

最佳答案

您需要在要为其提供错误消息的所有类上定义 IErrorInfo。

关于c# - 具有复杂类型的 IDataErrorInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1440766/

30 4 0
文章推荐: mysql - 如何从使用用户变量作为计数器的查询中获得正确的结果?
文章推荐: c# - VSTO Outlook 集成 - 用于同步的 Outlook 关闭事件
文章推荐: c# - 在 SQL 中实现 C# 迁移代码
文章推荐: php - 使用