Im using the following example and it the list which should need to do the validation to is currently
inside the validation rules class but now I need to get it from outside and the list can be changed during RT,
how can I send the list from the view model to the validation rules class
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>
I have a solution but it need to be tested: First you can not use directly the property validation in the binding, because it creates a new instance of the class and then you can not add items to the validation list. I create an instance in the resource base in the App.xaml file:
In this way you can access it from any part of the application, this is an example in a MainViewModel class that I have created:
But for doing this I need to create public methods in the
PropertValclass:Then filanlly use it in the xaml code:
Hope it helps. This is an idea I have not fully tested, but hope helps...