次の例を使用していますが、検証を行う必要があるリストは現在
検証ルールクラスの内部ですが、外部から取得する必要があり、リストは 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>