开发者

Setting Validation error template from code in WPF

开发者 https://www.devze.com 2023-01-27 08:59 出处:网络
I have a TextBox in my WPF app. I have defined a ControlTemplate for validation error as follows: <ControlTemplate x:Key=\"validationTemplate\">

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


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


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));


Validation.SetErrorTemplate(txtBox, this.FindResource("validationTemplate") as ControlTemplate);


For your first question. You can set the ErrorTemplate from code behind like.

    public MainWindow()
    {
        InitializeComponent();

        var template = this.FindResource("validationTemplate") as ControlTemplate;
        Validation.SetErrorTemplate(this.textBox1, template);
     }


Edit: For your second question. Please refer the following sample. sites.google.com/site/html5tutorials/ValidationErrorText.zip

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号