从 WPF 中的代码设置验证错误模板
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要在代码后面设置“Validation.HasError”,您可以使用 Validation.MarkInvalid 方法
取消设置您使用的值
To set "Validation.HasError" in code behind you can use the Validation.MarkInvalid method
To unset the value you use
感谢他向我推荐的精彩链接。我的代码有点这样
Thanks to for the wonderful link he suggested me.My code goes somewhat this way
对于你的第一个问题。您可以从代码后面设置 ErrorTemplate 。
编辑:
对于你的第二个问题。请参考以下示例。
sites.google.com/site/html5tutorials/ValidationErrorText.zip
For your first question. You can set the ErrorTemplate from code behind like.
Edit:
For your second question. Please refer the following sample.
sites.google.com/site/html5tutorials/ValidationErrorText.zip