Add a new row immediately after adding a new item

I have a datagrid like this:

<DataGrid
    ItemsSource="{Binding Things}" 
    AutoGenerateColumns="False">

    <DataGrid.Columns>
        <DataGridTextColumn Header="Whatever" Binding="{Binding Blah}" />
    </DataGrid.Columns>     
</DataGrid>

Things and things look like this:

public ObservableCollection<Thing> Things { get; set; }

.

public class Thing
{
    public string Blah { get; set; }

    public Thing() { }
}

The default behavior for a datagrid is to start with a new empty line at the bottom. When I start editing any property of this line, a new thing is created and added to the Things collection. However, a new empty row is not displayed until I finish editing (i.e. press the enter button or select a new row / column). Is there a way (preferably one that does not violate MVVM) to make this new empty line right after editing starts?

This is the starting point:

Starting point

This is after double-clicking an empty line and editing:

Editing

When I finish editing, a new empty line will appear:

New blank line

But here is what I want (New blank line when editing):

New Blank Line While Editing

+4
1

, datagrid PreparingCellForEdit, , .

, ( + 1), , .

, , .

, PreparingCellForEdit, ( ) , .

DataGrid dataGrid = sender as DataGrid;

if (dataGrid == null)
{
    return; 
}

int currentRow = e.Row.GetIndex();

if (dataGrid.Items.Count - 1 <= currentRow)
{
    Things.Add(new Thing());
}

, . ObservableCollection, , .

, Thing , .

+2

All Articles