79097242

Date: 2024-10-17 08:51:40
Score: 2.5
Natty:
Report link

The issue is not the DataGrid, but that the DataGridColumn already has an DataGridOwner when it is added a second time. The solution is to clear the DataGridOwner property of the DataGridColumn like Rune Anderson posted in another question.

Thanks to Rune Anderson who posted this answer in a comment in How do I bind a WPF DataGrid to a variable number of columns?

    Table.Columns.Clear();
    foreach (var column in columns)
    {
        var dataGridOwnerProperty = column.GetType().GetProperty("DataGridOwner", BindingFlags.Instance | BindingFlags.NonPublic); 
        dataGridOwnerProperty?.SetValue(column, null); 
        Table.Columns.Add(column);
    }

note: this is also what @mtopp says in his comment.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Blacklisted phrase (1): another question
  • Blacklisted phrase (1): How do I
  • Whitelisted phrase (-1): solution is
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @mtopp
  • Low reputation (1):
Posted by: Sjoerd Spieker