I have an (part of) XAML
file like this
<TextBox.Text>
<Binding Path="MyProperty"
UpdateSourceTrigger="PropertyChanged"
TargetNullValue="">
<Binding.ValidationRules>
<validation:IntRangeRule Min="-999"
Max="999" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
with IntRangeRule
class like this
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value != null)
{
var stringToConvert = value as string;
if (!string.IsNullOrEmpty(stringToConvert))
{
Int32 number;
bool result = Int32.TryParse(stringToConvert, NumberStyles.Integer, cultureInfo, out number);
if (!result)
{
var localizer = ServiceLocator.Current.GetInstance<IResourceLocalizer>();
return new ValidationResult(false, string.Format(localizer["IntValidationError"], stringToConvert, Min, Max));
}
if ((number < Min) || (number > Max))
{
var localizer = ServiceLocator.Current.GetInstance<IResourceLocalizer>();
return new ValidationResult(false, string.Format(localizer["IntRangeValidationError"], Min, Max));
}
}
}
return new ValidationResult(true, 0);
}
Since I realized that when ValidationResult
has first parameter false
it does not changes the MyProperty
property.
So, my goal is to somehow acknowledge, inside the ViewModel, is the ValidationResult
true or false, so I can use that information inside my if
statement. I could not find how to do this so far.