aspnetcore
aspnetcore copied to clipboard
Use property attributes to define text in the header of QuickGrid.
Is there an existing issue for this?
- [X] 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:
- [DisplayName] of property.
- [Display] of property.
- [DisplayName] of base property.
- [Display] of base property.
- 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.