См. Следующую ссылку, это именно то, что
http://blogs.msdn.com/b/vinsibal/archive/2008/11/05/wpf-datagrid-new-item-template-sample.aspx
Обновление 2
Немного изменил ControlTemplate
и загрузил мой пример проекта здесь:
http://www.mediafire.com/download.php?ea519xwbc53i91i
Обновление
Добавление необходимых шагов по ссылке
Xaml
<DataGrid x:Name="dataGrid"
LoadingRow="dataGrid_LoadingRow"
UnloadingRow="dataGrid_UnloadingRow"
RowEditEnding="dataGrid_RowEditEnding"
...>
<DataGrid.Resources>
<ControlTemplate x:Key="NewRow_ControlTemplate" TargetType="{x:Type DataGridRow}">
<Border x:Name="DGR_Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
<SelectiveScrollingGrid>
<TextBlock Text="Click here to add a new item." Grid.Column="1"/>
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseLeftButtonDown" Handler="Row_MouseLeftButtonDown" />
</Style>
</DataGrid.RowStyle>
<!--...-->
</DataGrid>
Код позади
ControlTemplate _defaultRowControlTemplate = null;
ControlTemplate _newRowControlTemplate = null;
private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (_defaultRowControlTemplate == null)
{
_defaultRowControlTemplate = e.Row.Template;
}
if (_newRowControlTemplate == null)
{
_newRowControlTemplate = dataGrid.FindResource("NewRow_ControlTemplate") as ControlTemplate;
}
if (e.Row.Item == CollectionView.NewItemPlaceholder)
{
e.Row.Template = _newRowControlTemplate;
e.Row.UpdateLayout();
}
}
private void dataGrid_UnloadingRow(object sender, DataGridRowEventArgs e)
{
if (e.Row.Item == CollectionView.NewItemPlaceholder && e.Row.Template != _defaultRowControlTemplate)
{
e.Row.Template = _defaultRowControlTemplate;
e.Row.UpdateLayout();
}
}
private void Row_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridRow row = sender as DataGridRow;
if (row.Item == CollectionView.NewItemPlaceholder && row.Template == _newRowControlTemplate)
{
// for a new row update the template and open for edit
row.Template = _defaultRowControlTemplate;
row.UpdateLayout();
dataGrid.CurrentItem = row.Item;
DataGridCell cell = DataGridHelper.GetCell(dataGrid, dataGrid.Items.IndexOf(row.Item), 0);
cell.Focus();
dataGrid.BeginEdit();
}
}
private void dataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
IEditableCollectionView iecv = CollectionViewSource.GetDefaultView((sender as DataGrid).ItemsSource) as IEditableCollectionView;
if (iecv.IsAddingNew)
{
// need to wait till after the operation as the NewItemPlaceHolder is added after
Dispatcher.Invoke(new DispatcherOperationCallback(ResetNewItemTemplate), DispatcherPriority.ApplicationIdle, dataGrid);
}
}
private object ResetNewItemTemplate(object arg)
{
DataGridRow row = DataGridHelper.GetRow(dataGrid, dataGrid.Items.Count - 1);
if (row.Template != _newRowControlTemplate)
{
row.Template = _newRowControlTemplate;
row.UpdateLayout();
}
return null;
}
DataGridHelper
public static class DataGridHelper
{
public static DataGridCell GetCell(DataGrid dataGrid, int row, int column)
{
DataGridRow rowContainer = GetRow(dataGrid, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
if (presenter == null)
{
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
}
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public static DataGridRow GetRow(DataGrid dataGrid, int index)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
object hh = dataGrid.Items[index];
// may be virtualized, bring into view and try again
dataGrid.ScrollIntoView(hh);
dataGrid.UpdateLayout();
row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
}