Silverlight 验证不适用于数据注释
我有一个表单:
<StackPanel x:Name="LayoutRoot">
<sdk:ValidationSummary />
<sdk:Label Target="{Binding ElementName=Greeting}" />
<TextBox x:Name="Greeting" Text="{Binding Greeting, Mode=TwoWay,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<sdk:Label Target="{Binding ElementName=Name}" />
<TextBox x:Name="Name" Text="{Binding Name, Mode=TwoWay,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
</StackPanel>
一个简单的类被设置为 DataContext...
public class Person : INotifyPropertyChanged
{
private string _greeting;
private string _name;
public string Greeting
{
get { return _greeting; }
set
{
_greeting = value;
InvokePropertyChanged(new PropertyChangedEventArgs("Greeting"));
}
}
[Required(ErrorMessage = "Name must be provided")]
[StringLength(15, MinimumLength = 5,
ErrorMessage = "Name should be 5 to 15 characters")]
public string Name
{
get { return _name; }
set
{
_name = value;
InvokePropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
}
我在 xaml 后面的代码中使用以下行设置数据上下文:
DataContext = new Person {Name = "Joe User"};
我看到表单上的数据,名称的标签是粗体,表示必填。但是,如果我清空该字段,或将其设置为无效长度的字符串,则标签本身或验证摘要不会得到验证。我知道文本框在失去焦点之前不会验证,因此我单击问候语字段并输入文本以确保我已离开其他文本控件。
我在这里缺少什么?
答案:
根据 @Alex Paven 的答案,要让它与数据注释一起使用,您可以使用:
[Required(ErrorMessage = "Name must be provided")]
[StringLength(15, MinimumLength = 5,
ErrorMessage = "Name should be 5 to 15 characters")]
public string Name
{
get { return _name; }
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
_name = value;
InvokePropertyChanged(new PropertyChangedEventArgs("DisplayName"));
}
}
至于 IDataErrorInfo,我会研究它。谢谢!
I have a form:
<StackPanel x:Name="LayoutRoot">
<sdk:ValidationSummary />
<sdk:Label Target="{Binding ElementName=Greeting}" />
<TextBox x:Name="Greeting" Text="{Binding Greeting, Mode=TwoWay,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<sdk:Label Target="{Binding ElementName=Name}" />
<TextBox x:Name="Name" Text="{Binding Name, Mode=TwoWay,
ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
</StackPanel>
And a simple class this is set as the DataContext...
public class Person : INotifyPropertyChanged
{
private string _greeting;
private string _name;
public string Greeting
{
get { return _greeting; }
set
{
_greeting = value;
InvokePropertyChanged(new PropertyChangedEventArgs("Greeting"));
}
}
[Required(ErrorMessage = "Name must be provided")]
[StringLength(15, MinimumLength = 5,
ErrorMessage = "Name should be 5 to 15 characters")]
public string Name
{
get { return _name; }
set
{
_name = value;
InvokePropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
}
I set the data context with the following line in the code behind of the xaml:
DataContext = new Person {Name = "Joe User"};
I see the data on the form, and the label for Name is bold, indicating required. However, if I empty the field, or set it to a string of an invalid length, I get no validation, on the label itself, or the validation summary. I understand the textbox doesn't validate until lost focus, so I click into the greeting field and enter text to make sure I've left the other text control.
What am I missing here?
Answer:
Per @Alex Paven's answer, to get it to work with Data Annotations you would use:
[Required(ErrorMessage = "Name must be provided")]
[StringLength(15, MinimumLength = 5,
ErrorMessage = "Name should be 5 to 15 characters")]
public string Name
{
get { return _name; }
set
{
Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name" });
_name = value;
InvokePropertyChanged(new PropertyChangedEventArgs("DisplayName"));
}
}
As for IDataErrorInfo, I'll look into it. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错过了实际的验证调用。使用 ValidatesOnExceptions,必须在属性设置器中引发异常,并且在验证时不会自动考虑属性。为了使其工作,您需要使用正确的参数调用 System.ComponentModel.DataAnnotations.Validator.ValidateProperty。
但是,如果使用 Silverlight 4,我建议考虑使用 IDataErrorInfo 进行验证,因为我觉得它提供了更多的灵活性。
You're missing the actual validation call. With ValidatesOnExceptions, an exception must be thrown in the property setter, and the attributes are not taken into account automatically with respect to validation. For it to work you need a call to System.ComponentModel.DataAnnotations.Validator.ValidateProperty with the correct parameters.
However, if using Silverlight 4, I'd suggest looking into validating with IDataErrorInfo, as I feel it offers a lot more flexibility.