如何解决在WPF DataGrid中手动阻止新行
美好的一天。如何以编程方式防止在数据网格中创建新行?例如:
private void SomeDataGridEvent()
if (MyBoolConditionIsTrue())
Prevent_Grid_Row_Creation_Logic
else
//Do Nothing. The new row is created
编辑1: 注意:CanUserDeleteRows必须为TRUE。用户可以添加行,但前提是满足某些条件。
解决方法
您可以这样做:
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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。