Setting Validation error template from code in WPF

前端 未结 4 1287
醉话见心
醉话见心 2021-02-13 22:40

I have a TextBox in my WPF app. I have defined a ControlTemplate for validation error as follows:


    

        
4条回答
  •  囚心锁ツ
    2021-02-13 23:01

    Thanks to for the wonderful link he suggested me.My code goes somewhat this way

    String errorMessage = GetFormattedErrorMessage(toolTip.Range, range);
    ValidationError validationError = new ValidationError(new DummyValidator(),
    txtBox.GetBindingExpression(TextBox.TextProperty));
    Validation.MarkInvalid(txtBox.GetBindingExpression(TextBox.TextProperty), validationError);
    validationError.ErrorContent = errorMessage;
    Validation.SetErrorTemplate(txtBox, GetErrorTemplate(errorMessage));
    

提交回复
热议问题