3

I got an DataGrid in MVVM which´s ItemsSource is bound to a Custom Model.

The properties in this model are named like their equivalent in the database. For example this:

public string StapelStatus_Txt
    {
        get
        {
            return this.stapelstatusTxt;
        }
        set
        {
            this.stapelstatusTxt = value;
            this.OnPropertyChanged("StapelStatus_Txt");
        }
    }

Is it somehow possible (without renaming the propertie) to declare another displayname for the datagrid? I thought the data anotation "DisplayName" would help... but it don´t.

Someone got an idea?:)

Kind regards

Robert Nagel
  • 149
  • 14

2 Answers2

10

You can, for example, use DisplayNameAttribute on property and set column header like this:

<DataGrid AutoGeneratingColumn="OnAutoGeneratingColumn"/>

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    e.Column.Header = ((PropertyDescriptor)e.PropertyDescriptor).DisplayName;
}
Stipo
  • 4,566
  • 1
  • 21
  • 37
0

One way to do it could be

<DataGrid AutoGeneratingColumn="DataGrid_AutoGeneratingColumn">

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    e.Column.Header = MyPropertyNameConverter.Convert(e.PropertyName);
}
Dtex
  • 2,593
  • 1
  • 16
  • 17