How To Validation This Class?(WPF)
How To Validation This Class?(WPF)
I can not understand is the Property Value for each.
For this method : public override ValidationResul开发者_StackOverflow中文版t Validate(object value.
name maximum char must be 10; age maximum value must be 150;
public class Person : ValidationRule
{
string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
int _age = 20;
public int Age
{
get { return _age; }
set { _age = value; }
}
string _Phone = "000-0000";
public string Phone
{
get { return _Phone; }
set { _Phone = value; }
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
**//is value Which Property?**
//I can not understand is the Property Value for each
return new ValidationResult(true, null);
}
}
You can't make your class derive from ValidationRule
: it's a person, not a rule.
First, I don't recommend that WPF developers use validation rules at all. Use MVVM, and have your view model implement IDataErrorInfo
as described (for instance) here.
If you want to create a single ValidationRule
class to validate your Person
class, you can, but you'll need to create a PropertyName
property on the class and set it in your XAML, e.g.:
<TextBox>
<TextBox.Text>
<Binding Path="Age"
Mode="TwoWay">
<Binding.ValidationRules>
<local:PersonValidationRule PropertyName="Age"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Then the Validate
method in this class can look at the PropertyName
and branch accordingly. Of course, now you've implemented a new point of failure - what happens if you put down the wrong property name in your XAML? If you use data-error validation, that can't happen.
精彩评论