question

FanhuaKong-4325 avatar image
0 Votes"
FanhuaKong-4325 asked ·

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

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FanhuaKong-4325 avatar image
0 Votes"
FanhuaKong-4325 answered ·

Hi, I found the answer on

[1]: https://stackoverflow.com/questions/10342715/validationrule-with-validationstep-updatedvalue-is-called-with-bindingexpressi


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;
             }
         }
     }


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.