我正在使用System.ComponentModel.DataAnnotations我的Entity Framework 4.1项目进行验证.
例如:
public class Player
{
[Required]
[MaxLength(30)]
[Display(Name = "Player Name")]
public string PlayerName { get; set; }
[MaxLength(100)]
[Display(Name = "Player Description")]
public string PlayerDescription{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要检索Display.Name注释值以在消息中显示它,例如所选的"玩家名称"是Frank.
================================================== ===============================
我需要检索注释的另一个例子:
var playerNameTextBox = new TextBox();
playerNameTextBox.MaxLength = GetAnnotation(myPlayer.PlayerName, MaxLength);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在尝试找到验证MVVM中数据的最佳方法.目前,我正在尝试使用MVVM模式将IDataErrorInfo与Data Annotations一起使用.
然而,似乎没有任何工作,我不知道我可能做错了什么.我有类似的东西.
public class Person : IDataErrorInfo
{
[Required(ErrorMessage="Please enter your name")]
public string Name { get; set; }
public string Error
{
get { throw new NotImplementedException(); }
}
string IDataErrorInfo.this[string propertyName]
{
get
{
return OnValidate(propertyName);
}
}
protected virtual string OnValidate(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("Property may not be null or empty", propertyName);
string error = string.Empty;
var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
var results = new List<ValidationResult>();
var context = new ValidationContext(this, null, …Run Code Online (Sandbox Code Playgroud)