dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.06k stars 1.17k forks source link

Support DataAnnotations and TagHelpers in WPF #2256

Open VBAndCs opened 4 years ago

VBAndCs commented 4 years ago

I suggest that you allow using the attributes defined in System.ComponentModel.DataAnnotations with any property in a wpf project, like ASP.NET does. For example:

[Required]
[MinLength(2)]
[MaxLength(12)]
public string FirstName { get; set; }

ASP.NET generates server side validation code for these attributes (beside the client side validation scripts), so, I think you can make WPF borrow the class that generates the server side code to achieve this. Note: There are some attributes used with tag helpers to affect the UI such as DisplayAttribute. I think XAML should also add the tag helpers concept too. I find it easier and shorter than the currently used data binding.

dragnilar commented 4 years ago

+1 on this as well, coming from MVC, I find it very weird not having out of the box support for Data Annotation validation. I feel like this is one of the spots where WPF/XAML could really use some love.

ghost commented 4 years ago

I am skeptical on this one. Where would you use these? A lot of the attributes seem really web-centric.

bukowa commented 4 days ago

Hey, it's been a while, I am just learning WPF and landed on this issue, I think it may be a good time to ask if anything changed and for some other ways to reuse that attribute validation logic?

miloush commented 4 days ago

Nothing changed as far as I know. You could create a ValidationRule that reads any ValidationAttributes on the target and validates the value against them.

The attributes are in a separate assembly that WPF would have to introduce a dependency on, I am not sure that is a realistic expectation at the moment.