Auto generating columns

Mar 13, 2009 at 1:17 PM
I'm using AutoGeneratingColumn event to set columns in DataGrid. When the ItemsSource is set to an empty collection this event is not raised and so DataGrid is rendered with no columns. In case if CanUserAddRows set to true there is no way for user to add a row. There is an AddAutoColumns() method in DataGrid class which prevents column autogeneration when there are no items in ItemsSource.

private void AddAutoColumns()
    if (ItemsSource != null &&
        ItemsSource is INotifyCollectionChanged &&
        DataItemsCount == 0)
        // do deferred generation
        DeferAutoGeneration = true;
    else if (!_measureNeverInvoked)
        DataGrid.GenerateColumns((IItemProperties)Items, this, null);

Simply adding !CanUserAddRows to the first if() statement will fix the problem, but to be honest I can't see the point in skipping columns generation when ItemsSource is empty, regardless of CanUserAddRows value.

kind regards,
Mar 13, 2009 at 7:24 PM
There were issues with not having a representative item and retrieving the item properties. I have filed a bug in our internal database to look at this again since there were some bug fixes in the next version of WPF dealing with representative items, which may allow us to enable this scenario in a future version.