Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Validation error template from code in WPF

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 ??

like image 735
Gurucharan Balakuntla Maheshku Avatar asked Nov 25 '10 05:11

Gurucharan Balakuntla Maheshku


3 Answers

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
like image 97
Fredrik Hedblad Avatar answered Oct 13 '22 21:10

Fredrik Hedblad


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));
like image 5
Gurucharan Balakuntla Maheshku Avatar answered Oct 13 '22 23:10

Gurucharan Balakuntla Maheshku


Validation.SetErrorTemplate(txtBox, this.FindResource("validationTemplate") as ControlTemplate);
like image 4
decyclone Avatar answered Oct 13 '22 23:10

decyclone