Custom ValidationAttribute doesn't work. Always returns true

喜你入骨 提交于 2019-12-05 22:31:57

The reason why your custom ValidationAttribute class isn't working is because WPF doesn't (by default) look into such classes when doing validation. The default mechanism for validation is implementing the IDataErrorInfo (available for .NET 4.0 and earlier) or INotifyDataErrorInfo (introduced in .NET 4.5) interfaces. If you don't want to implement any of those interfaces, then you can create a ValidationRule, but I prefer implementing the interfaces mentioned above.

You can find a lot of examples on how to do this online, but doing a quick search found this blog post (which on a quick scan I felt was very thorough).


EDIT

Since you seemed more keen on using Data Annotations instead of IDataErrorInfo/INotifyDataErrorInfo interfaces or Validation Rule, I think the Microsoft TechNet article "Data Validation in MVVM" is a very clean and thorough implementation of using Data Annotations for validation. I read through the solution myself and would recommend it to others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!