我正在使用以下示例,它当前需要进行验证的列表是
在验证规则类中,但现在我需要从外部获取它,并且可以在 RT 期间更改列表,
如何将列表从视图模型发送到验证规则类
public class PropertVal : ValidationRule
{
private readonly List<String> validValues = new List<String> { "aa", "bb", "cc", "dd" };
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if(value == null)
return new ValidationResult(false, "The Field are not match");
string val = value.ToString().Trim();
bool isValid = !string.IsNullOrEmpty(val) && validValues.Contains(val);
ValidationResult result = null;
result = isValid
? new ValidationResult(true, null)
: new ValidationResult(false, "The Field are not match");
return result;
}
}
XAML
<TextBox>
<TextBox.Text>
<Binding Path="NameOfViewModelPropery" UpdateSourceTrigger="PropertyChanged"
>
<Binding.ValidationRules>
<local:PropertiesMapValidation ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>