2012-11-27 44 views
2

在AddNewRecord行中輸入值後,單擊XamDataGrid行外的任何位置似乎都會將該行添加到集合中。AddNewRecord XamDataGrid

如何防止鼠標點擊將新行添加到集合中。

請在AddNewRecord的任何幫助

+0

任何Infragistics的大師(S),請分享你的想法 – user977606

+0

我想只通過點擊+圖標AddNewRecord – user977606

+0

添加新行,請幫助請幫助 – user977606

回答

2

點擊外端上的記錄編輯模式,如果有變化,他們在那個時候,這意味着新的記錄添加提交。按回車通過單擊網格中的另一個記錄鍵而不是當如果你希望只允許記錄被commmited,那麼你可以使用下面的邏輯來設置鼠標左鍵向下的處理:

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; 
     } 
    }   
} 
+0

很好的答案,謝謝。 – MikeMalter

0

感謝您的回答@alhalama!

我注意到,雖然你不處理鼠標右鍵,甚至當我們做你的解決方案無法支持它。另外,使用您的解決方案,我無法編輯任何其他單元格,直到我在添加新行記錄(可能是某些人想要的,但不是我)上按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(); 
     } 
    } 
}