I do not know if it is the right solution, but I came up to something like this, in desperation:
foreach (NavField field in this.Fields)
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = field.FieldNo.ToString();
//Some other logic
// Hide non active and hidden fields
if (!field.Active || !field.Show)
column.Visibility = System.Windows.Visibility.Collapsed;
grid.Columns.Add(column);
}
Then I add the datatable as itemssource:
this.dataGridLines.ItemsSource = dataTable.DefaultView;
If I set the datatable directly, it does not care about the columns from the datatable and autogenerate its own columns, don't know why..