wpf IDataErrorInfo :如何知道所有属性是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13603243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IDataErrorInfo : How to know if all properties are valid?
提问by J4N
I have a WPFapplication(.Net 3.5) which uses the IDataErrorInfoon the ViewModel to validate input.
我有一个WPF应用程序(.Net 3.5),它使用IDataErrorInfoViewModel 上的 来验证输入。
It works great, the usercontrol get the correct UI feedback.
效果很好,用户控件获得了正确的 UI 反馈。
The problem is that the user can still change the selected element, or save this element.
问题是用户仍然可以更改所选元素,或保存该元素。
So my question is: How can I know that all my properties are valid? Or at least that all my displayed values are valid. The goal is to bind some IsActiveon this result
所以我的问题是:我怎么知道我的所有属性都是有效的?或者至少我所有显示的值都是有效的。目标是IsActive在这个结果上绑定一些
回答by Bob Vale
From your comment on your implementation of IDataErrorInfochange your implementation to this style....
从您对您的实施的评论将您的实施IDataErrorInfo更改为这种风格....
#region IDataErrorInfo Members
public string Error
{
get { return this[null] }
}
public string this[string columnName]
{
get
{
StringBuilder result = new StringBuilder();
if (string.IsNullOrEmpty(columnName) || columnName == "FirstName")
{
if (string.IsNullOrEmpty(FirstName))
result.Append("Please enter a First Name\n");
}
if (string.IsNullOrEmpty(columnName) || columnName == "LastName")
{
if (string.IsNullOrEmpty(LastName))
result.Append("Please enter a Last Name\n");
}
if (string.IsNullOrEmpty(columnName) || columnName == "Age")
{
if (Age < = 0 || Age >= 99)
result.Append("Please enter a valid age\n");
}
return (result.Length==0) ? null : result.Remove(result.Length-1,1).ToString();
}
}
#endregion
public bool IsValid {
get { return string.IsNullOrEmpty(this.Error); }
}
Then in your property changed event
然后在你的财产改变事件中
if (e.PropertyName == "Error") {
OnPropertyChanged(this,new PropertyChangedEventArgs("IsValid"));
}
if (e.PropertyName != "Error" && e.PropertyName != "IsValid") {
OnPropertyChanged(this,new PropertyChangedEventArgs("Error"));
}
回答by J4N
For now, I added this method on my model.
现在,我在我的模型上添加了这个方法。
public Boolean IsModelValid()
{
Boolean isValid = true;
PropertyInfo[] properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo p in properties)
{
if (!p.CanWrite || !p.CanRead)
{
continue;
}
if (this[p.Name] != null)
{
isValid = false;
}
}
return isValid;
}
I bound the object itself on the PropertyChanged event,
我在 PropertyChanged 事件上绑定了对象本身,
public MyClassName()
{
PropertyChanged += CheckModelValidity;
CheckModelValidity(null, null);
}
when it change, I call this method, and if the result is different than my actual public member, I update it:
当它改变时,我调用这个方法,如果结果与我的实际公共成员不同,我更新它:
private void CheckModelValidity(object sender, PropertyChangedEventArgs e)
{
bool isModelValid = IsModelValid();
if(isModelValid!= IsValid)
{
IsValid = isModelValid;
}
}
And then I can just bind the IsValid property.
然后我可以绑定 IsValid 属性。
I don't know if there is a better solution?
不知道有没有更好的解决办法?

