Я пытался разрешить моей сетке данных получать данные из буфера обмена, но я продолжаю получать эту ошибку всякий раз, когда пытаюсь обновить sh элементы: InvalidOperationException: 'Refresh' is not allowed during an AddNew or EditItem transaction.
Это происходит даже после того, как я CommitEdit()
дважды. Кажется, что все привязки выстраиваются должным образом, когда я проверяю это.
Вот код вставки, он вызывается из команды на элементе управления. Вы нажимаете кнопку, и она вызывает это на пользовательском элементе управления DataGrid
.
public void Paste()
{
string text = Clipboard.GetData(DataFormats.Text) as string;
if (text == null)
{
return;
}
List<string[]> row_data =
text.Split('\n')
.Select(row =>
row.Split('\t')
.Select(cell =>
cell.Length > 0 && cell[cell.Length - 1] == '\r' ?
cell.Substring(0, cell.Length - 1) : cell).ToArray())
.Where(a => a.Any(b => b.Length > 0)).ToList();
this.Focus();
int min_row_index = Math.Max(Items.IndexOf(CurrentItem), 0);
int min_col_display_index = Math.Max((SelectionUnit != DataGridSelectionUnit.FullRow) ? Columns.IndexOf(CurrentColumn) : 0, 0);
int max_col_display_index = Columns.Count;
if (SelectedCells != null && SelectedCells.Count > 0) {
int column_index = SelectedCells.First().Column.DisplayIndex;
var row = SelectedCells.First().Item;
int row_index = Items.IndexOfReference(row);
min_row_index = CurrentItem == null ? row_index : Math.Min(row_index, min_row_index);
min_col_display_index = CurrentItem == null ? column_index : Math.Min(column_index, min_col_display_index);
}
else
{
min_col_display_index = 0;
}
IEditableCollectionView view = CollectionViewSource.GetDefaultView(Items) as IEditableCollectionView;
if (view == null) return;
BeginEditCommand.Execute(null, this);
var total_data = row_data.SelectMany(s => s).Count();
var item_count = Items.Count;
for (int i = 0; i < row_data.Count + 1 - (item_count - min_row_index); i++)
{
view.AddNew();
}
int row_data_index = 0;
for (int i = min_row_index;
row_data_index < row_data.Count;
i++,row_data_index++)
{
int col_data_index = 0;
for (int j = min_col_display_index;
j < max_col_display_index && col_data_index < row_data[row_data_index].Length;
j++, col_data_index++)
{
DataGridColumn c = ColumnFromDisplayIndex(j);
string property_name = ((c as DataGridBoundColumn).Binding as Binding).Path.Path;
object item = Items[i];
object val = row_data[row_data_index][col_data_index];
var property_info = item.GetType().GetProperty(property_name);
Tuple<PropertyInfo, object> property_object_pair = GetPropertyInfoFromBindingRecursive(item, property_name);
if (property_object_pair != null && val is string s && s == "") { continue; }
if (property_object_pair != null)
{
Debug.Assert(item == property_object_pair.Item2);
property_info = property_object_pair.Item1;
item = property_object_pair.Item2;
}
if (property_info == null) continue;
if (property_info.GetSetMethod() == null) continue;
DataGridColumn column = ColumnFromDisplayIndex(j);
Binding binding = ((column as DataGridBoundColumn)?.Binding ?? column.ClipboardContentBinding) as Binding;
if (binding == null)
{
Debug.Fail("Column needs ClipboardContentBinding");
continue;
}
object converted_value = Binding.DoNothing;
if (binding.Converter != null)
{
try
{
converted_value = binding.Converter.Convert (val, property_info.GetType (), binding.ConverterParameter, binding.ConverterCulture);
}
catch (Exception) { continue; }
}
else
{
try
{
converted_value = Convert.ChangeType (val, property_info.PropertyType);
}
catch (Exception) { continue; }
}
if (converted_value == Binding.DoNothing) { continue; }
property_info.SetValue(item, converted_value, null);
}
}
CommitEdit();
CommitEdit();
Items.Refresh();
}
Я понятия не имею, что с этим не так - есть идеи?