2

I am binding a grid with observableCollection of User defined type. My Class has some properties e.g. FirstName, LastName, DateOfBirth etc.

When I am binding Grid. It is displaying the same header i.e. FirstName but I want it to be like First Name.

I am sure there is something to do with attributes on the property in the class but I don't know which attribute should I use.

I have tried Display attribute but it did not work.

Any information will be helpful...

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Gyandeep
  • 119
  • 1
  • 11
  • Are You try this: http://stackoverflow.com/questions/151682/dynamically-setting-the-header-text-of-a-silverlight-datagrid-column – WooCaSh Dec 28 '12 at 06:26
  • Thanks WooCash, I am not binding the header anywhere and i don't want to do that. I have set autogeneratedcolumns to true because the same grid will be bound to different datasources. – Gyandeep Dec 28 '12 at 06:36

1 Answers1

2

Not sure if there is a way to do this in xaml, but you could add an EventHandler to and add some logic to change the ColumnHeader text.

xaml:

   <DataGrid ItemsSource="{Binding ...}" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" />

code:

  private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
  {
     e.Column.Header = string.Concat(e.Column.Header.ToString().Select(x => char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' '); 
  }

this will convert Pascal casing to have spaces between uppercase chars eg: "FirstName" = "First Name"

Before

After

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • Hey Can you give some suggestion on the ordering part of the properties so that it comes in some defined order. I can not have static column in the grid and also grid will be bound to multiple datasources which will different object collection – Gyandeep Dec 28 '12 at 11:17