RegisteredUser 实现 IDataErrorInfo 这个接口
问题:不理解这种表达方式,this代表什么, 后边用[]括起来又是什么意思 , 请解释一下
public string this[string propName]
- C# code
public partial class RegisteredUser:IDataErrorInfo { public string this[string propName] { get { if (propName == "UserName" && string.IsNullOrEmpty(LoginName)) return "用户登录名不能为空"; return string.Empty; } } public string Error { get { return string.Empty; } } }
- C# code
using System;using System.Reflection;namespace System.ComponentModel{ // 摘要: // 提供功能,该功能提供用户界面可以绑定的自定义错误信息。 public interface IDataErrorInfo { // 摘要: // 获取指示对象何处出错的错误信息。 // // 返回结果: // 指示对象何处出错的错误信息。默认值为空字符串 ("")。 string Error { get; } // 摘要: // 获取具有给定名称的属性的错误信息。 // // 参数: // columnName: // 要获取其错误信息的属性的名称。 // // 返回结果: // 该属性的错误信息。默认值为空字符串 ("")。 string this[string columnName] { get; } }}
------解决方案--------------------------------------------------------
this指对象本身,声明索引器,columnName指的属性名称
可以这样使用
RegisteredUser["UserName"]这种格式来显示错误信息
参见
this的用法
http://msdn.microsoft.com/zh-cn/library/dk1507sz.aspx
http://msdn.microsoft.com/zh-cn/library/6x16t2tx.aspx
------解决方案--------------------------------------------------------
public partial class RegisteredUser:IDataErrorInfo
partial 的意思就是部分,也就是这里只是类定义的一部分,另一个部分在*.designer.cs中。LoginName应该在后面的文件里。*.designer.cs里包含Designer中的内容,像拖个控件到Form上,就会修改*.designer.cs文件,因此自己写的代码不要放在里面。