【问题标题】:Wrong NewItemPlaceholder Behavior错误的 NewItemPlaceholder 行为
【发布时间】:2012-07-01 18:06:44
【问题描述】:

我对@9​​87654322@ 有一些问题。当DataGrid 设置为CanUserAddRows 时,一个新的黑色行被放置在DataGrid 的底部,但是这些行有一个意外的行为,因为如果我只是单击新行并将焦点传递给另一个单元格,则该行是即使它是空的,也会创建。如果该行为空,我想通过改变行为来避免创建新项目,但如果在事件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);
}

【问题讨论】:

  • 我取消 New,因为如果我不这样做,则会引发“AddItem 或 NewItem 事务不允许延迟刷新”的异常
  • 我推荐使用这个解决方案! stackoverflow.com/a/42763784/973344

标签: c# wpf


【解决方案1】:

我刚刚通过刷新 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-07
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多