从 WPF 中的代码设置验证错误模板

发布于 2024-10-04 11:54:09 字数 1843 浏览 0 评论 0原文

我的 WPF 应用程序中有一个文本框。我已经为验证错误定义了一个 ControlTemplate,如下所示:

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

我的 TextBox 如下:

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

现在,如果我的 TextBox 添加了 ValidationRule,然后我在那里进行验证,则错误模板将正确应用。但由于其他一些问题我不能这样做。

所以我必须验证 PreviewLostKeyboardFocus 中 TextBox 的内容。我正在验证文本框。现在我想在代码后面设置文本框的错误模板,但我无法做到这一点!

我尝试了这个,但它没有按预期工作::

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
    }

问题 1:我想知道如何将错误模板添加到 TextBox

问题 2:我想知道如何从代码设置控件模板的错误消息。例如,我想将默认错误消息“无效输入:”更改为“无效输入:请输入正确的输入”。

我只想在代码后面做上述事情!!!!

编辑1:

问题是我如何从代码后面将 Validation.HasError 设置为 true 因为我我没有使用任何验证器。 (或者我应该从应用 ValidationTemplate 背后的代码中设置什么??))

编辑2:

我正在进行 XML 绑定,所以我无法实现 IDataErrorInfo !我只想从代码后面实现这一点!有没有办法从代码后面设置 Validation.HasError ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在风中等你 2024-10-11 11:54:09

要在代码后面设置“Validation.HasError”,您可以使用 Validation.MarkInvalid 方法

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

取消设置您使用的值

Validation.ClearInvalid

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
你在我安 2024-10-11 11:54:09

感谢他向我推荐的精彩链接。我的代码有点这样

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

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));
好倦 2024-10-11 11:54:09
Validation.SetErrorTemplate(txtBox, this.FindResource("validationTemplate") as ControlTemplate);
Validation.SetErrorTemplate(txtBox, this.FindResource("validationTemplate") as ControlTemplate);
痞味浪人 2024-10-11 11:54:09

对于你的第一个问题。您可以从代码后面设置 ErrorTemplate 。

    public MainWindow()
    {
        InitializeComponent();

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

编辑:
对于你的第二个问题。请参考以下示例。
sites.google.com/site/html5tutorials/ValidationErrorText.zip

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文