从WPF中的代码设置验证错误模板 [英] Setting Validation error template from code in WPF
问题描述
我的WPF应用程序中有一个TextBox.我为验证错误定义了一个ControlTemplate,如下所示:
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>
我的文本框如下:
<TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}">
<TextBox.Text>
<Binding Path="TEXT1" ValidatesOnDataErrors="True" validatesOnExceptions="True">
</Binding>
</TextBox.Text>
</TextBox>
现在,如果我的文本框已添加ValidationRule,然后在此进行验证,则错误模板将正确应用.但是由于其他一些问题,我无法做到这一点.
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.
因此,我必须在PreviewLostKeyboardFocus中验证TextBox的内容.我正在验证TextBox.现在,我想在后面的代码中为TextBox设置错误模板,但是我无法做到!
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
}
问题1:我想知道如何将错误模板添加到TextBox
Question 1: I want to know how to add the error template to TextBox
问题2:我想知道如何从代码中设置控制模板的错误消息.例如,我想将默认错误消息无效输入:"更改为无效输入:请输入正确的输入".
我只想在代码后面做上述事情!!!!
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 !!!!
问题是如何从Validation.HasError后面的代码中将其设置为true,因为我没有使用任何Validator. (或者我应该从应用ValidationTemplate后面的代码中设置什么?)
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 ?? ))
我正在执行XML绑定,因此无法实现IDataErrorInfo!我只想从后面的代码中实现!有没有一种方法可以从??后面的代码中设置Validation.HasError?
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 ??
推荐答案
要在后面的代码中设置"Validation.HasError",您可以使用Validation.MarkInvalid方法
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);
}
要取消设置您使用的值
Validation.ClearInvalid
这篇关于从WPF中的代码设置验证错误模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!