Hi, I found the answer on
Based on what is said in the above post. When you change the ValidationStep into UpdatedValue or CommittedValue. WPF encapsulate the property's value into a BindingExpression. I think maybe that's why you can update source data without the validation. Just like the Expression, it includes all data and will execute later.
So, if you want to change the validation warning sign around the control, you should get useful value from the BindingExpression like following:
public class SampleValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string str = GetBoundValue(value) as string;
bool result = RegularExpr.TransformerNumberRegex(str);
if (result)
return new ValidationResult(true, null);
else
return new ValidationResult(false, "The string format should be like [SS1-1]");
}
private object GetBoundValue(object value)
{
if (value is BindingExpression)
{
// ValidationStep was UpdatedValue or CommittedValue (validate after setting)
// Need to pull the value out of the BindingExpression.
BindingExpression binding = (BindingExpression)value;
// Get the bound object and name of the property
string resolvedPropertyName = binding.GetType().GetProperty("ResolvedSourcePropertyName", BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).GetValue(binding, null).ToString();
object resolvedSource = binding.GetType().GetProperty("ResolvedSource", BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).GetValue(binding, null);
// Extract the value of the property
object propertyValue = resolvedSource.GetType().GetProperty(resolvedPropertyName).GetValue(resolvedSource, null);
return propertyValue;
}
else
{
return value;
}
}
}