错误的NewItemPlaceholder行为

时间:2012-07-01 18:06:45

标签: c# wpf

DataGrid我遇到了一些问题。当DataGrid设置为CanUserAddRows时,新的黑色行位于DataGrid的底部,但这些行有意外的行为,因为如果我只是clik新行并传递焦点对于另一个单元格,即使该行为空,也会创建该行。如果行为空,我想通过非常改变行为,避免创建新项目,但如果事件RowEditEnding我设置e.Cancel=true,那么NewItemPlaceHolder desapears我不能从那时起添加任何行。有没有人找到这些问题的答案?

protected override void OnRowEditEnding(DataGridRowEditEndingEventArgs e)
{
    if ((e.Row.Item as DataRowView).Row.ItemArray[0] == null || (e.Row.Item as DataRowView).Row.ItemArray[0].ToString() == String.Empty)
    {
        e.Cancel = true;

        IEditableCollectionView collection = Items as IEditableCollectionView;

        if (collection.IsAddingNew)
        {
            collection.CancelNew();
        }
    }            
    base.OnRowEditEnding(e);
}

1 个答案:

答案 0 :(得分:3)

我刚刚通过刷新CanUserAddRows属性找到了一种方法

bool canUserAddRows = Datagrid.CanUserAddRows;

                //Makes the refresh for CanUserAddRows because when cancel the new adding then collapse the NewPlaceHolder item
                Datagrid.CanUserAddRows = !canUserAddRows;
                Datagrid.CanUserAddRows = canUserAddRows;