Search code examples
c#.netwpfxamldatagrid

xaml Translate column names in Datagrid


I use a DataGrid which is bound to an ObservableCollection<T> like this:

<DataGrid ItemsSource="{Binding Path=Items}" />   

The column names in the grid are the same as the names of the properties in my class Items. How can I change these names, e.g. based on current culture?

My Item class has the properties:

public object ThisIsProperty1{get;set;}
public object Property2{get;set;}

My column names in the grid get the name "ThisIsProperty1" and "Property2", but I want to set customized names like "P1" and "Prop 2".

Thanks in advance!


Solution

  • If you only want to override some Headers, try the following:

    <DataGrid ItemsSource="{Binding Path=Items}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Desired Header" Binding="{Binding SomePropToOverride}"/>
            <DataGridTextColumn Header="Another Header" Binding="{Binding SomeOtherPropToOverride}"/>
        </DataGrid.Columns>
    </DataGrid>
    

    If you only want specific columns to show you can add AutoGenerateColumns="false" to the DataGrid

    You can find a more in depth tutorial here