I'm using a standard .NET DataGrid like this:
<DataGrid ItemsSource="{Binding Datensaetze}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="my col 1" Binding="{Binding MyCol1}"/>
<DataGridTextColumn Header="my col 2" Binding="{Binding MyCol2}"/>
<DataGridTextColumn Header="my col 3" Binding="{Binding MyCol3}"/>
</DataGrid.Columns>
</DataGrid>
This is working nicely. Now I want to define the columns in the ViewModel, and instead of setting fixed columns in xaml I want to generate them on the fly. However, if I try to bind the Columns to anything I get an error, saying
DataGrid.Columns is a readonly property and can't be bound.
Is there a way to dynamically bind the DataGrid columns to something in code behind?
Yes, the Columns Property is ReadOnly so we can't bind to it directly. If you want to Bind Columns then you can try to use an attached property which you bind to, which in turn updates the Columns.
Update
Using delta of changes in the CollectionChanged event.
Then you can bind the BindableColumns property to your ColumnsCollection