WPF 验证错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15199567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Validation Errors
提问by Max
In my current projet, I have to deal with data validation in a WPF form. My form is in a DataTemplate in a ResourceDictionnary. I can save and load the data from my form thanks to two buttons, which serialize and deserialize the data (through two DelegateCommand).
在我当前的项目中,我必须处理 WPF 表单中的数据验证。我的表单位于 ResourceDictionnary 中的 DataTemplate 中。由于两个按钮,我可以从我的表单保存和加载数据,这些按钮序列化和反序列化数据(通过两个DelegateCommand)。
If one field of my form is empty or invalid, the save button is disable. A field is checked everytime it changes thanks to the UpdateSourceTrigger propertie. That's why I need to know in my C# code if a field is invalid to update my save command.
如果我的表单中有一个字段为空或无效,则保存按钮将被禁用。由于 UpdateSourceTrigger 属性,每次更改时都会检查字段。这就是为什么我需要在我的 C# 代码中知道一个字段是否无效来更新我的保存命令。
Currently, I use the ExceptionValidationRule in my XAML Binding and I wonder if it's a good pratice. I can't implement a ValidationRule because I need to know in my C# code if a field is invalid, to update the save command (enable or disable the save button).
目前,我在 XAML 绑定中使用了 ExceptionValidationRule,我想知道这是否是一个好的实践。我无法实现 ValidationRule 因为我需要在我的 C# 代码中知道一个字段是否无效,以更新保存命令(启用或禁用保存按钮)。
<TextBox>
<Binding Path="Contact.FirstName" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox>
In this blog, we can read :
在这篇博客中,我们可以阅读:
Raising exceptions in the Setters is not a very good approach since those properties are also set by code and sometimes it is ok to temporarily leave them with error values.
在 Setter 中引发异常并不是一个很好的方法,因为这些属性也是由代码设置的,有时可以暂时为它们保留错误值。
I already read this postbut I can't use it, my TextBox are in a DataTemplate and I can't use them in my C# code.
我已经阅读了这篇文章,但我无法使用它,我的 TextBox 位于 DataTemplate 中,我无法在我的 C# 代码中使用它们。
So, I wonder if I should change my Data Validation and don't use the ExceptionValidationRule.
所以,我想知道我是否应该更改我的数据验证并且不使用 ExceptionValidationRule。
回答by Max
Thank you blindmeis, your idea was good. IDataErrorInfo seems to be better than the ExceptionValidationException and it works.
谢谢盲人,你的想法很好。IDataErrorInfo 似乎比 ExceptionValidationException 更好,并且它有效。
Here is an example which match my project : IDataErrorInfo sample
这是一个与我的项目匹配的示例: IDataErrorInfo 示例
It doesn't use the DelegateCommand but is simple enough to be modified. Your model has to implement IDataErrorInfo :
它不使用 DelegateCommand 但很简单,可以修改。您的模型必须实现 IDataErrorInfo :
public class Contact : IDataErrorInfo
{
public string Error
{
get { throw new NotImplementedException(); }
}
public string Name { get; set; }
public string this[string property]
{
get
{
string result = null;
if (property== "Name")
{
if (string.IsNullOrEmpty(Name) || Name.Length < 3)
result = "Please enter a Name";
}
return result;
}
}
}
In the XAML code, don't forget to change the Binding :
在 XAML 代码中,不要忘记更改 Binding :
<TextBox>
<Binding Path="Contact.Name" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True" NotifyOnValidationError="True"/>
</TextBox>

