2

在 AddNewRecord 行中输入值后,单击 XamDataGrid 上该行之外的任意位置似乎将该行添加到集合中。

如何防止鼠标单击将新行添加到集合中。

请提供任何帮助

4

2 回答 2

2

在 AddNewRecord 之外单击会结束记录的编辑模式,如果有更改,那么此时会提交更改,这意味着添加了新记录。如果您希望仅在按下回车键时允许提交记录,而不是通过单击网格中的另一条记录,那么您可以使用以下逻辑将鼠标左键设置为已处理:

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;
        }
    }           
}
于 2012-12-31T20:56:07.560 回答
0

感谢@alhalama的回答!

我注意到尽管您没有处理鼠标右键,即使我们执行您的解决方案也无法支持它。此外,使用您的解决方案,我无法编辑任何其他单元格,直到我在 Add New Row 记录上按 Enter 或 Escape (这可能是某些人想要的,但不是我)。这是我修改后的解决方案,当用户单击它时撤消对“添加新记录”行的单元格的更改,它还处理所有鼠标单击(左、右、中等)。

// 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();
        }
    }  
}
于 2014-02-05T22:17:24.623 回答