dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.

Home Page:https://asp.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use property attributes to define text in the header of QuickGrid.

FelipeCostaGualberto opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Currently, QuickGrid's PropertyColumn gets the name of the property and use it as a title in the header of the table, as shown here.

It would be nice to get the text of the header based on the property's metadata.

Describe the solution you'd like

I suggest to get the text of the title following these rules, in this order of priority or another you judge better:

  1. [DisplayName] of property.
  2. [Display] of property.
  3. [DisplayName] of base property.
  4. [Display] of base property.
  5. Name of the property.

So, the code would be something like:

            var propertyExpression = (MemberExpression)Property.Body;
            var propertyName = propertyExpression.Member.Name;
            var propertyType = propertyExpression.Expression.Type;
            var propertyInfo = propertyType.GetProperty(propertyName);

            Title =
                propertyInfo.GetCustomAttributes<DisplayNameAttribute>(false).FirstOrDefault()?.DisplayName ??
                propertyInfo.GetCustomAttributes<DisplayAttribute>(false).FirstOrDefault()?.Name ??
                propertyInfo.GetCustomAttributes<DisplayNameAttribute>(true).FirstOrDefault()?.DisplayName ??
                propertyInfo.GetCustomAttributes<DisplayAttribute>(true).FirstOrDefault()?.Name ??
                propertyName;

In my tests, this works fine with localization if you declare a property like this:

    [Display(Name = nameof(AppResources.Name), ResourceType = typeof(AppResources))]
    public string CustomerName { get; set; }

Additional context

No response

Thanks for contacting us. Moving this to the backlog for now to gauge customer demand in the future.
The complexity here is that if we choose to do something like this, we'll need to use DisplayName for other parts of the framework too, rather than just this particular property.

As for now, you can subsclass the PropertyColumn class instead and implement what you need.