这是一个严肃的问题,因为对于我2天后的生活,我仍然无法在WPF应用程序中使用Data Annotations来处理我的类。而且似乎每篇涉及数据注释的Google文章都提到了ASP,MVC或其他一些我没有使用的基于Web的东西。我已按照之前给出的所有建议,但验证不起作用。
我正在使用实体框架数据库第一种方法和使用MVVMLight库的MVVM模式。 (如果这与它有关,我不知道。)
我已经按照人们的说法添加了“伙伴”课程。
public class MemberMetadata
{
[Required(AllowEmptyStrings = false, ErrorMessage = "Forename is required.")]
public string Forename;
}
[MetadataType(typeof(MemberMetadata))]
public partial class Member
{
}
我在应用程序加载时注册了这个类。 (在App.xaml.cs构造函数中)
TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Member), typeof(MemberMetadata)), typeof(Member));
我的绑定设置如下:
<ContentControl Template="{StaticResource LabelledTextBox}"
Content="Forename: "
DataContext="{Binding Path=SelectedMember.Forename,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True,
NotifyOnValidationError=True,
ValidatesOnNotifyDataErrors=True,
ValidatesOnDataErrors=True,
Mode=TwoWay}"/>
我还需要做什么?或者说,我应该怎么做呢?因为在方法中编写40 if语句或为我需要的每个验证规则创建一个类是完全荒谬的。
答案 0 :(得分:0)
在您的课程中,为属性添加注释并从PropertyChangedNotification
继承。另外,实现INotifyPropertyChanged
和IDataErrorInfo
接口。