WPF Simple Validation Question - setting custom ErrorContent

后端 未结 2 1780
情话喂你
情话喂你 2021-01-02 01:28

If I have the following TextBox:



        
2条回答
  •  自闭症患者
    2021-01-02 02:03

    You can use ValidationRules.

    For instance, in my case, when a user enters an invalid value into a decimal datagridtextcolumn, instead of the default message "Value could not be converted" I can override it with:

    
        
            
                
                    
                
            
        
    
    

    and here's the code for the DecimalRule:

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        decimal convertedDecimal;
        if (!decimal.TryParse((string)value, out convertedDecimal))
        {
            return new ValidationResult(false, "My Custom Message"));
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
    

提交回复
热议问题