【问题标题】:Manually prevent new rows in WPF DataGrid手动阻止 WPF DataGrid 中的新行
【发布时间】:2020-12-02 23:42:06
【问题描述】:

美好的一天。如何以编程方式防止在数据网格中创建新行?例如:

private void SomeDataGridEvent()
   if (MyBoolConditionIsTrue())
      Prevent_Grid_Row_Creation_Logic
   else
     //Do Nothing. The new row is created

编辑1: 注意:CanUserDeleteRows 必须为 TRUE。用户可以添加行,但前提是某些条件为真。

【问题讨论】:

标签: c# .net wpf datagrid


【解决方案1】:

你可以这样做:

AddingNewItem="DataGrid_AddingNewItem"

private void DataGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
{
    if (sender is DataGrid dg)
    {
        if (MyBoolConditionIsTrue())
        {
            dg.Items.Remove(e.NewItem);
        }
        else
        {
            //Do Nothing. The new row is created
        }
    }
}

或者

RowEditEnding="DataGrid_RowEditEnding"

private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    if (MyBoolConditionIsTrue())
    {
        e.Cancel = true;
    }
    else
    {
        //Do Nothing. The new row is created
    }
}

【讨论】:

  • 正确答案是“DataGrid_RowEditEnding”。谢谢!
猜你喜欢
  • 2018-10-25
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 2012-12-09
  • 2012-03-12
  • 1970-01-01
相关资源
最近更新 更多