When ValidationStep set to UpdatedValue, the invalid red border won't go off

Fanhua Kong 241 Reputation points
2020-02-28T11:54:04.897+00:00

Hi guys.
I'm using ValidationRules to do the data check on UI. When I set the ValidationStep to UpdatedValue, it works weird. When the input is incorrect, the control shows a red border as normally. However, after I changed the value correctly, the red border won't go off. Why is this happening?
As far as I know, in UpdatedValue mode, the validation happens after the source value been updated. That is to say when I changed the input into correct one, the source object's property value changed as well. So in this case, the value in UI and in source are both correct. Why is the red border still there?
By the way, things work fine in RawProposedValue mode.
Thanks a lot.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
0 comments No comments
{count} votes

Accepted answer
  1. Fanhua Kong 241 Reputation points
    2020-02-29T09:01:54.223+00:00

    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;  
                }  
            }  
        }  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful