在WPF开发中最近遇到一个很变态的问题:快速点击Datagrid之后,再对Datagrid操作时会出现如下异常
private void dtgSchemeApp_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (IsUnderTabHeader(e.OriginalSource as DependencyObject))
{
CommitTables(this.LV_Points);
}
}
private bool IsUnderTabHeader(DependencyObject control)
{
if (control is Border)
return true;
DependencyObject parent = VisualTreeHelper.GetParent(control);
if (parent == null)
return false;
return IsUnderTabHeader(parent);
}
private void CommitTables(DependencyObject control)
{
if (control is DataGrid)
{
DataGrid grid = control as DataGrid;
grid.CommitEdit(DataGridEditingUnit.Row, true);
return;
}
int childrenCount = VisualTreeHelper.GetChildrenCount(control);
for (int childIndex = 0; childIndex CommitTables(VisualTreeHelper.GetChild(control, childIndex));
}