WPF - DataGrid DisplayName

THe DataGrid Element can be used to display a data source like a List or a data table. You might create a List of a Model class to gather data from a database or some other source and prepare it for further work. If you then want to display this List as data source in a WPF or even WinForm DataGrid element you could work with WPF AutogeneratedColumns. This feature turns the List Property names to your column names in the DataGrid. If these are rather technical, you might like to convert them to a certain language or to be more user friendly.

This might be your Model class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Product 
{
    [DisplayName(ProductConstants.Locked)]
    public int Locked { get; set; }

    [DisplayName(ProductConstants.LockedBy)]
    public string LockedBy { get; set; }

    [DisplayName(ProductConstants.LockedComment)]
    public string LockedComment { get; set; }

    [DisplayName(ProductConstants.LockedTimestamp)]
    public DateTime? LockedTimestamp { get; set; }

    [DisplayName(ProductConstants.UnlockedBy)]
    public string UnlockedBy { get; set; }

    [DisplayName(ProductConstants.UnlockedComment)]
    public string UnlockedComment { get; set; }

    [DisplayName(ProductConstants.UnlockedTimestamp)]
    public DateTime? UnlockedTimestamp { get; set; }

    [DisplayName(ProductConstants.Product)]
    public string Product { get; set; }
}

You’ll need the using System.ComponentModel;. The [DisplayName()] Attributes can contain some String to be displayed instead of the property name. In this example I used to have a ProductConstants class with string constants for a small project.