更新时间:2023-12-04 12:48:22
对于那些感兴趣的人-尝试更新可绑定对象的值的列似乎出了点问题-可能的数据类型转换,所以我自己实现了这一点
For those interested - there does seem to be something going wrong with the columns attempt to update the value of the bindable object - possible data type conversion so i have implemented this myself and it works like a charm now.
protected virtual void OnExecutedPaste(object sender, ExecutedRoutedEventArgs args)
{
// parse the clipboard data
List<string[]> rowData = ClipboardHelper.ParseClipboardData();
bool hasAddedNewRow = false;
// call OnPastingCellClipboardContent for each cell
int minRowIndex = Math.Max(Items.IndexOf(CurrentItem), 0);
int maxRowIndex = Items.Count - 1;
int minColumnDisplayIndex = (SelectionUnit != DataGridSelectionUnit.FullRow) ? Columns.IndexOf(CurrentColumn) : 0;
int maxColumnDisplayIndex = Columns.Count - 1;
int rowDataIndex = 0;
for (int i = minRowIndex; i <= maxRowIndex && rowDataIndex < rowData.Count; i++, rowDataIndex++)
{
if (CanUserAddRows && i == maxRowIndex)
{
// add a new row to be pasted to
ICollectionView cv = CollectionViewSource.GetDefaultView(Items);
IEditableCollectionView iecv = cv as IEditableCollectionView;
if (iecv != null)
{
hasAddedNewRow = true;
iecv.AddNew();
if (rowDataIndex + 1 < rowData.Count)
{
// still has more items to paste, update the maxRowIndex
maxRowIndex = Items.Count - 1;
}
}
}
else if (i == maxRowIndex)
{
continue;
}
int columnDataIndex = 0;
for (int j = minColumnDisplayIndex; j < maxColumnDisplayIndex && columnDataIndex < rowData[rowDataIndex].Length; j++, columnDataIndex++)
{
DataGridColumn column = ColumnFromDisplayIndex(j);
string propertyName = ((column as DataGridBoundColumn).Binding as Binding).Path.Path;
object item = Items[i];
object value = rowData[rowDataIndex][columnDataIndex];
PropertyInfo pi = item.GetType().GetProperty(propertyName);
if (pi != null)
{
object convertedValue = Convert.ChangeType(value, pi.PropertyType);
item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null);
}
//column.OnPastingCellClipboardContent(item, rowData[rowDataIndex][columnDataIndex]);
}
}
}