DataGrid Blank Row Missing?

Sep 23, 2009 at 3:10 PM

I am creating a WPF window with a DataGrid, and I want to show the blank "new item" row at the bottom of the grid that allows me to add a new item to the grid. For some reason, the blank row is not shown on the grid on my window. Here is the markup I used to create the DataGrid:

<toolkit:DataGrid  x:Name="ProjectTasksDataGrid" 
                   DockPanel.Dock="Top" 
                   Style="{DynamicResource {x:Static res:SharedResources.FsBlueGridKey}}"
                   AutoGenerateColumns="False" 
                   ItemsSource="{Binding SelectedProject.Tasks}" 
                   RowHeaderWidth="0" 
                   MouseMove="OnStartDrag" 
                   DragEnter="OnCheckDropTarget" 
                   DragOver="OnCheckDropTarget" 
                   DragLeave="OnCheckDropTarget" 
                   Drop="OnDrop" 
                   InitializingNewItem="ProjectTasksDataGrid_InitializingNewItem">
	<toolkit:DataGrid.Columns>
        <toolkit:DataGridCheckBoxColumn HeaderTemplate="{DynamicResource {x:Static res:SharedResources.CheckmarkHeaderKey}}" Width="25" Binding="{Binding Completed}" IsReadOnly="false"/>
        <toolkit:DataGridTextColumn Header="Days" Width="75" Binding="{Binding NumDays}" IsReadOnly="false"/>
        <toolkit:DataGridTextColumn Header="Due Date" Width="75" Binding="{Binding DueDate, Converter={StaticResource standardDateConverter}}" IsReadOnly="false"/>
        <toolkit:DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" IsReadOnly="false"/>
	</toolkit:DataGrid.Columns>
</toolkit:DataGrid>

I can't figure out why the blank row isn't showing. I have tried the obvious stuff (IsReadOnly="false", CanUserAddRows="True"), with no luck. Any idea why the blank row is disabled? Thanks for your help.

Oct 28, 2009 at 3:45 PM

Finally got back to this one. Turns out the problem lies with the collection class I use. The class is derived from ObservableCollection<T>, but it has two type parameters:

public class VmCollection<VM, DM> : ObservableCollection<VM>
{
     ...
}

The class wraps a domain object (DM) inside a view model object wrapper (VM), then adds the wrapper to its collection.

It seems that the second type parameter causes the WPF DataGrid to become uneditable. The fix is to avoid calling the VmCollection class at run time. Instead, I derive a class for each type of object to be wrapped:

public class FooItemsVm : VmCollection <FooItemVm, FooItem>
{
     ...
}

The derived class is a simple shell, much like a Repository derived class. Since the class has no typed parameters, it appears to solve the problem.

I have documented the problem and the fix here. I have also written a CodeProject article on the wrapper classes, which I am going to update for this fix. Hope this helps someone else down the road!