I'm using the WPF DataGrid to bind to a collection of a custom class. Using AutoGenerateColumns="True" in the grid XAML, the grid is created and populated just fine, but the headings are the property names, as one would expect.
I tried specifying
<Display(Name:="My Name")>
from the System.ComponentModel.DataAnnotations namespace and that has no effect. I also tried
<DisplayName("My Name")>
from the System.ComponentModel name space but still the headings are not affected.
Is there no way to specify column headings with the AutoGenerateColumns option?
Best Answer
Using @Marc's suggestion was the beginning of the solution, but taken on it's own, the AutoGenerated columns still have the property names as headings.
To get the DisplayName, you need to add a routine (in the code behind) to handle the GridAutoGeneratingColumn event:
An additional and better solution is to use the ComponentModel.DataAnnotations namespace and specify ShortName:
OnGeneratingColumn becomes:
Note that the order of attributes in the attribute array changes, so you must use the GetType(...) instead of a numeric parameter... Such fun!