CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
2.8k stars 277 forks source link

Property HasErrors of ObservableValidator is auto-generated in controls with auto-generation #881

Open arivoir opened 1 month ago

arivoir commented 1 month ago

Describe the bug

When binding an object inheriting ObservableValidator class to a control that has auto-generation a field is created for that property.

For instance when binding to WPF DataGrid the following column appears image

Regression

No response

Steps to reproduce

1. Create a WPF Project
2. Add a DataGrid element to the page
3. Create an object inheriting ObservableValidator
4. Create a collection of the items in step 3.
5. Set the collection in the ItemsSource property of the DataGrid created in step 2.

Expected behavior

No column should be generated for the property HasErrors

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

Sergio0694 commented 1 month ago

It's not clear at all why this is a bug, and the issue only describes some WPF-specific behavior. You'd need to include more details on what exact change you'd expect on ObservableValidator, and why, for this to be actionable.

arivoir commented 1 month ago

Hi @Sergio0694 , please let me give more details about the context.

ObservableValidator is a class that can be used to add validation to objects and these objects can be bound to ui-controls. These controls many time auto-generate the ui based on the information in the class itself, for instance, WPF DataGrid take all the properties of the class and creates the columns out of this.

This works fine in most scenarios, but in other cases it is necessary to tweak this mechanism so the classes are bound to control seamlessly without having to write complex customizations. For this purpose exists the System.ComponentModel.DataAnnotations attributes. https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.displayattribute?view=net-8.0

In this particular case, it is the HasErrors property that should be decorated with DisplayAttribute, so when it is bound to a control this property doesn't generate ui

    /// <inheritdoc/>
    [Display(AutoGenerateField = false)]
    public bool HasErrors => this.totalErrors > 0;

It worth noting this is not only in WPF, but any ui framework of control using System.ComponentModel.DataAnnotations, and it's probably not a bug but a feature enhancement.

arivoir commented 1 month ago

@Sergio0694 I just created a PR with the fix/feature enhancement. I appreciate if you can take a look. Thanks