Setting Validation error template from code in WPF

GuruC picture GuruC · Nov 25, 2010 · Viewed 14.8k times · Source

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

<ControlTemplate x:Key="validationTemplate">
    <DockPanel LastChildFill="True">
         <TextBlock DockPanel.Dock="Bottom"  Text="Invalid Input: "></TextBlock>
                 <AdornedElementPlaceholder />
    </DockPanel>
</ControlTemplate>

My TextBox is as follows :

<TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}">                                              
    <TextBox.Text>
        <Binding Path="TEXT1" ValidatesOnDataErrors="True" validatesOnExceptions="True">
         </Binding>
    </TextBox.Text>
</TextBox>

Now if my TextBox is added ValidationRule and then I validate there, the error template applies correctly. But I cant do that because of some other problem.

So I have to validate the content of TextBox in PreviewLostKeyboardFocus. I am validating the TextBox. Now I want to set the error template for the TextBox in code behind but I am unable to do it !!

I tried this but it does not work as intented::

private void blockTextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        TextBox txtBox = sender as TextBox;
        txtBox.Template = this.FindResource("validationTemplate") as ControlTemplate;

        //this behaves strange; it removes the TextBox and places the ErrorTemplate. 
       //I want it to behave like the way WPF does internally wherein it places 
       //the error template around TExtBox
    }

Question 1: I want to know how to add the error template to TextBox

Question 2: I want to know how do I set the error message of the control template from code. Like for example, I want to change the default error message "Invalid Input: " to "Invalid Input: Please enter correct input".

I want to do the above mentioned things in code behind only !!!!

EDIT 1:

The problem is how do I set from code behind Validation.HasError as true because I am not using any Validator. (or what should I set from code behind that ValidationTemplate gets applied ?? ))

EDIT 2:

I am doing XML binding so there is no way I can implement IDataErrorInfo !! I want to achieve this from code behind only!! Is there a way to set Validation.HasError from code behind ??

Answer

Fredrik Hedblad picture Fredrik Hedblad · Nov 26, 2010

To set "Validation.HasError" in code behind you can use the Validation.MarkInvalid method

private void blockTextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
{ 
    TextBox txtBox = sender as TextBox;
    //...
    BindingExpression bindingExpression =
        BindingOperations.GetBindingExpression(txtBox, TextBox.TextProperty);

    BindingExpressionBase bindingExpressionBase = 
        BindingOperations.GetBindingExpressionBase(txtBox, TextBox.TextProperty);

    ValidationError validationError =
        new ValidationError(new ExceptionValidationRule(), bindingExpression);

    Validation.MarkInvalid(bindingExpressionBase, validationError);
}

To unset the value you use

Validation.ClearInvalid