在AddNewRecord行中输入一个值后,单击XamDataGrid上行外的任何位置似乎将该行添加到集合中。
如何阻止鼠标点击向集合中添加新行。
请帮助
答案 0 :(得分:2)
在AddNewRecord外部单击会结束记录上的编辑模式,如果有更改,则会在此时提交,这意味着将添加新记录。如果您只想在按Enter键时允许记录,而不是通过单击网格中的其他记录,那么您可以使用以下逻辑将鼠标左键设置为已处理:
private bool editingAddNewRecord = false;
void XamDataGrid1_EditModeEnded(object sender, Infragistics.Windows.DataPresenter.Events.EditModeEndedEventArgs e)
{
this.editingAddNewRecord = false;
}
void XamDataGrid1_EditModeStarted(object sender, Infragistics.Windows.DataPresenter.Events.EditModeStartedEventArgs e)
{
this.editingAddNewRecord = e.Cell.Record.IsAddRecord;
}
void XamDataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (editingAddNewRecord)
{
DataRecordPresenter drp = Infragistics.Windows.Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (!(drp != null && drp.IsAddRecord))
{
e.Handled = true;
}
}
}
答案 1 :(得分:0)
感谢@alhalama的回答!
我注意到你没有按下鼠标右键,即使我们做你的解决方案也无法支持它。此外,使用您的解决方案,我无法编辑任何其他单元格,直到我在Add New Row记录上点击Enter或Escape(这可能是某些人想要的,但不是我)。这是我修改过的解决方案,当用户点击它时,撤消对Add New Record行的单元格的更改,这也会处理所有鼠标点击(左,右,中等)。
// Used to record when the user is editing a value in the Mass Edit row.
private DataRecord _addRecordCellBeingEdited = null;
private void XamDataGrid1_EditModeStarted(object sender, Infragistics.Windows.DataPresenter.Events.EditModeStartedEventArgs e)
{
if (e.Cell.Record.IsAddRecord)
_addRecordCellBeingEdited = e.Cell.Record;
}
private void XamDataGrid1_EditModeEnded(object sender, Infragistics.Windows.DataPresenter.Events.EditModeEndedEventArgs e)
{
_addRecordCellBeingEdited = null;
}
private void XamDataGrid1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (_addRecordCellBeingEdited != null)
{
DataRecordPresenter drp = Infragistics.Windows.Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof(DataRecordPresenter), true) as DataRecordPresenter;
if (!(drp != null && drp.IsAddRecord))
{
_addRecordCellBeingEdited.CancelUpdate();
}
}
}