
我基本上得到了数据库优先的实体框架POCO对象,我正在为使用IDataErrorInfo提供验证.
现在这个工作正常,除了我有一个200行长的索引器,里面有大约40个if语句. (我是认真的.)
我现在正在做的是像这样扩展类:
public partial class MyPocoObject : IDataErrorInfo{ public string Error { get { throw new NotImplementedException("IDataErrorInfo.Error"); } } public string this[string columnname] { get { string result = null; // There's about 40 if statements here... } }} 显然,这是错误的,所以我尝试使用DataAnnotations.
这是我到目前为止所理解的.
我创建了元数据类,如下所示:
[MetadataType(typeof(MyObjectMetaData))]public partial class MyObject { }public class MyObjectMetaData{ [required(AllowEmptyStrings = false,ErrorMessage = "Forename is a required fIEld.")] public string Forename;} 然后我将控件声明为:
<TextBox Text="{Binding Selectedobject.Forename,NotifyOnValIDationError=True,ValIDatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"/> 然后我在别处有一个触发器:
<Style targettype="TextBox" BasedOn="{StaticResource Global}"> <Style.Triggers> <Trigger Property="ValIDation.HasError" Value="true"> <Setter Property="tooltip" Value="{Binding relativeSource={x:Static relativeSource.Self},Path=(ValIDation.Errors)[0].ErrorContent}"/> </Trigger> </Style.Triggers></Style> 当我使用IDataErrorInfo执行此 *** 作时,验证失败时,我会得到一个红色边框和一个带有错误消息的工具提示.使用数据注释我什么都没得到.
我应该如何实现这个?因为单个方法中的40个if语句是疯狂的.
更新:
我已经尝试了答案中建议的代码不起作用,虽然我认为我做错了.
public partial class Member : IDataErrorInfo{ // Error: The type 'Project.ClIEnt.Models.Member' already contains a deFinition for 'Forename' [required(AllowEmptyStrings = false,ErrorMessage = "Forename is a required fIEld.")] public string Forename; private Readonly Dictionary<string,object> _values = new Dictionary<string,object>(); public string Error { get { throw new NotImplementedException("IDataErrorInfo.Error"); } } public string this[string columnname] { get { return OnValIDate(columnname); } } protected virtual string OnValIDate(string propertyname) { if (string.IsNullOrEmpty(propertyname)) { throw new ArgumentException("InvalID property name",propertyname); } string error = string.Empty; // Error: Project.ClIEnt.Models.Member.GetValue<T>(string)' cannot be inferred from the usage. Try specifying the type arguments explicitly var value = GetValue(propertyname); var results = new List<System.ComponentModel.DataAnnotations.ValIDationResult>(1); var result = ValIDator.TryValIDateProperty( value,new ValIDationContext(this,null,null) { Membername = propertyname },results); if (!result) { var valIDationResult = results.First(); error = valIDationResult.ErrorMessage; } return error; } protected T GetValue<T>(string propertyname) { if (string.IsNullOrEmpty(propertyname)) { throw new ArgumentException("InvalID property name",propertyname); } object value; if (!_values.TryGetValue(propertyname,out value)) { value = default(T); _values.Add(propertyname,value); } return (T)value; }} 更新2:
MVVM上的文章中链接的代码似乎有效,但即使OnValIDate正在触发,也不会显示任何错误消息.
虽然现在还有其他错误…即使文本框的内容发生了变化,GetValue返回的值也总是相同的,并且在未从我的数据库加载的空对象上,验证根本不会触发.
解决方法 您似乎必须手动集成DataAnnotations验证.也许System.ComponentModel.DataAnnotations.ValIDator默认情况下不使用MetadataType,但在TypeDescriptor中注册它就像我之前的答案一样应该有效.Article
Code
我的意思是你必须实现你的valIDate方法并使用System.ComponentModel.DataAnnotations.ValIDator.
在我链接的源代码的PropertyChangednotification实现中获取一个战利品:
/// <summary> /// ValIDates current instance propertIEs using Data Annotations. /// </summary> /// <param name="propertyname">This instance property to valIDate.</param> /// <returns>Relevant error string on valIDation failure or <see cref="System.String.Empty"/> on valIDation success.</returns> protected virtual string OnValIDate(string propertyname) { if (string.IsNullOrEmpty(propertyname)) { throw new ArgumentException("InvalID property name",propertyname); } string error = string.Empty; var value = GetValue(propertyname); var results = new List<System.ComponentModel.DataAnnotations.ValIDationResult>(1); var result = ValIDator.TryValIDateProperty( value,null) { Membername = propertyname },results); if (!result) { var valIDationResult = results.First(); error = valIDationResult.ErrorMessage; } return error; } 总结 以上是内存溢出为你收集整理的c# – 如何显示DataAnnotations的错误消息全部内容,希望文章能够帮你解决c# – 如何显示DataAnnotations的错误消息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)