C# IDataErrorInfo:未验证输入
我有一个包含文本框的用户控件。我有一个名为Person的类,它实现IDataErrorInfo接口:C# IDataErrorInfo:未验证输入,c#,wpf,validation,idataerrorinfo,C#,Wpf,Validation,Idataerrorinfo,我有一个包含文本框的用户控件。我有一个名为Person的类,它实现IDataErrorInfo接口: class Person : IDataErrorInfo { private bool hasErrors = false; #region IDataErrorInfo Members public string Error { get { string
class Person : IDataErrorInfo
{
private bool hasErrors = false;
#region IDataErrorInfo Members
public string Error
{
get
{
string error = null;
if (hasErrors)
{
error = "xxThe form contains one or more validation errors";
}
return error;
}
}
public string this[string columnName]
{
get
{
return DoValidation(columnName);
}
}
#endregion
}
现在usercontrol公开了一个名为SetSource的方法,代码通过该方法设置绑定:
public partial class TxtUserControl : UserControl
{
private Binding _binding;
public void SetSource(string path,Object source)
{
_binding = new Binding(path);
_binding.Source = source;
ValidationRule rule;
rule = new DataErrorValidationRule();
rule.ValidatesOnTargetUpdated = true;
_binding.ValidationRules.Add(rule);
_binding.ValidatesOnDataErrors = true;
_binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
_binding.NotifyOnValidationError = true;
_binding.ValidatesOnExceptions = true;
txtNumeric.SetBinding(TextBox.TextProperty, _binding);
}
...
}
包含用户控件的WPF窗口具有以下代码:
public SampleWindow()
{
person= new Person();
person.Age = new Age();
person.Age.Value = 28;
numericAdmins.SetSource("Age.Value", person);
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
if(!String.IsNullOrEmpty(person.Error))
{
MessageBox.Show("Error: "+person.Error);
}
}
给定这段代码,绑定工作正常,但验证从未被触发。此代码有什么问题?您的
Age
类需要实现IDataErrorInfo
。您的Person
类不会被要求验证年龄的属性
如果这不是一个选项,我为WPF编写了一个验证系统,该系统具有足够的可扩展性来支持这一点。网址如下:
ZIP中有一个描述它的word文档
编辑:age有一种方法可以在不太聪明的情况下实现IDataErrorInfo:
class Constraint
{
public string Message { get; set; }
public Func<bool> Validate;
public string Name { get; set; }
}
class Age : IDataErrorInfo
{
private readonly List<Constraint> _constraints = new List<Constraint>();
public string this[string columnName]
{
get
{
var constraint = _constrains.Where(c => c.Name == columnName).FirstOrDefault();
if (constraint != null)
{
if (!constraint.Validate())
{
return constraint.Message;
}
}
return string.Empty;
}
}
}
class Person
{
private Age _age;
public Person()
{
_age = new Age(
new Constraint("Value", "Value must be greater than 28", () => Age > 28);
}
}
类约束
{
公共字符串消息{get;set;}
公共职能验证;
公共字符串名称{get;set;}
}
班级年龄:IDataErrorInfo
{
私有只读列表_约束=新列表();
公共字符串此[string columnName]
{
得到
{
var constraint=_constraint.Where(c=>c.Name==columnName.FirstOrDefault();
if(约束!=null)
{
如果(!constraint.Validate())
{
返回约束消息;
}
}
返回字符串。空;
}
}
}
班主任
{
私人年龄;
公众人士()
{
_年龄=新时代(
新约束(“值”,“值必须大于28”,“年龄>28”);
}
}
另请参见此链接:
另一种选择是在年龄类上实现IDataErrorInfo,并在其上创建一个事件,如Person类应该捕获的OnValidationRequested
。通过这种方式,您可以根据Person类的其他属性验证年龄字段。Paul,我看了一下您开发的框架。是吗还有其他方法吗?如果你能在这里提供一些指针,那将是GreatSave Age实现IDataErrorInfo或使用WPF验证规则(UI级验证)-这些是我能想到的唯一解决办法。在我的验证框架中,你只需将UI控件与规则名称关联(例如,“AgeValue”),并在IDataErrorInfo中处理“AgeValue”规则的验证。在我的框架中,属性名称和验证规则名称之间没有WPF验证中的耦合。谢谢,我能够使用从ValidationRule类派生的自定义验证规则获得所需的结果