Monday, December 31, 2012

Grid Binding Column Names

Grid Binding Column Names

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...

Answers & Comments...

Answer: 1

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

by : sa_ddam213http://stackoverflow.com/users/1849109




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog