我有一个数据网格,其中删除图标为一列,更新图标为另一列.单击更新时,第一个单元格设置为焦点.
单击删除我想删除所选行,但我收到错误"当ItemsSource正在使用时操作无效.请改为使用ItemsControl.ItemsSource访问和修改元素." 使用以下代码:
XAML:
C#:
private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e) { object source = e.OriginalSource; if (source.GetType() == typeof(Image)) { grdList.IsReadOnly = false; selectedRow = FindParent(sender as DependencyObject); if (((DataGridCell)sender).Column.Header.ToString().ToUpperInvariant() == "DELETE") { grdList.Items.Remove(selectedRow); } else { DataGridCellsPanel panel = FindVisualChild (selectedRow); DataGridCell dgc = panel.Children[0] as DataGridCell; dgc.Focus(); grdList.BeginEdit(); e.Handled = true; } } }
另外如何使用"删除"键添加删除功能以及单击删除单元格.