I am using Xceed's wpf
property grid control to show some of my configuration properties. I am doing via { SelectedObject="{Binding Entity.Configuration} }
where Configuration
object contains list of properties and this object is created at runtime using xml file.
I need to do validation on these properties (eg max/min values). However I didn't find any way of doing validation. Can anyone let me know if there is any?
Add the following to your class:
using System.ComponentModel.DataAnnotations;
public class YourClass : DataErrorInfoImpl
{
[Range(0, 100 , ErrorMessage = "The number must be from [0,100].")]
Double SomeNumberToValidate {get;set;}
}
public class DataErrorInfoImpl : IDataErrorInfo
{
string IDataErrorInfo.Error { get { return string.Empty; } }
string IDataErrorInfo.this[string columnName]
{
get
{
var pi = GetType().GetProperty(columnName);
var value = pi.GetValue(this, null);
var context = new ValidationContext(this, null, null) { MemberName = columnName };
var validationResults = new List<ValidationResult>();
if (!Validator.TryValidateProperty(value, context, validationResults))
{
var sb = new StringBuilder();
foreach (var vr in validationResults)
{
sb.AppendLine(vr.ErrorMessage);
}
return sb.ToString().Trim();
}
return null;
}
}
}
Disclosure: I pulled some of this code out of propertytools property grid. It works with both Xceed and PropertyTools library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.