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:
<Application x:Class="WpfApplicationTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplicationTest="clr-namespace:WpfApplicationTest" StartupUri="MainWindow.xaml">
<Application.Resources>
<WpfApplicationTest:PropertVal x:Key="propertVal" />
</Application.Resources>
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:
public class MainViewModel
{
public void AddValue()
{
var propVal = App.Current.MainWindow.Resources["propertVal"] as PropertVal;
if (propVal == null)
return;
propVal.AddListItem("Some New Item");
}
}
But for doing this I need to create public methods in the PropertVal
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;
}
public void AddListItem(string item)
{
//...
}
public void RemoveItem(string item)
{
//...
}
}
Then filanlly use it in the xaml code:
<TextBox>
<TextBox.Text>
<Binding Path="NameOfViewModelPropery" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<StaticResource ResourceKey="propertVal"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Hope it helps. This is an idea I have not fully tested, but hope helps...