Binding to double field with validation

前端 未结 6 946
时光说笑
时光说笑 2020-12-30 04:13

I\'m trying to bind TextBox to double property of some object with UpdateSourceTrigger=PropertyChanged. The goal is to immediately dur

6条回答
  •  暖寄归人
    2020-12-30 04:16

    I have run into the same problem, and have found a quite simple solution: use a custom validator, which does not return "valid" when the Text ends in "." or "0":

    double val = 0;
    string tmp = value.ToString();
    
    if (tmp.EndsWith(",") || tmp.EndsWith("0") || tmp.EndsWith("."))
    {
        return new ValidationResult(false, "Enter another digit, or delete the last one.");
    }
    else
    {
        return ValidationResult.ValidResult;
    }
    

提交回复
热议问题