Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 4 tasks
arivoir opened this issue May 28, 2024 · 4 comments · Fixed by #884
Closed
1 of 4 tasks
Labels
mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit needs author feedback 📝 This issue or PR needs a reply from the author

Comments

@arivoir
Copy link

arivoir commented May 28, 2024

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

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

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

@arivoir arivoir added the bug 🐛 An unexpected issue that highlights incorrect behavior label May 28, 2024
@Sergio0694
Copy link
Member

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.

@Sergio0694 Sergio0694 added needs author feedback 📝 This issue or PR needs a reply from the author mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit and removed bug 🐛 An unexpected issue that highlights incorrect behavior labels May 28, 2024
@arivoir
Copy link
Author

arivoir commented May 29, 2024

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
Copy link
Author

arivoir commented May 31, 2024

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

@arivoir
Copy link
Author

arivoir commented Aug 9, 2024

Thanks @Sergio0694!

paulomorgado pushed a commit to paulomorgado/CommunityToolkit-dotnet that referenced this issue Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit needs author feedback 📝 This issue or PR needs a reply from the author
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants