Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF MVVM DataGridColumnHeader DisplayName

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

like image 763
Robert Nagel Avatar asked Feb 22 '26 10:02

Robert Nagel


1 Answers

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;
}
like image 90
Stipo Avatar answered Feb 24 '26 06:02

Stipo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!