我最近开始将 WPF Datagrid 与包含 WPF AutoCompleteBox 的 DataGridTemplateColumns 一起使用,但在为这些 DataGridTemplateColumns 实现 Clipboard.Paste 功能时遇到了麻烦。
我已经设法通过 Vishal的指南让 Clipboard.Paste 与内置 DataGridColumns 一起工作,但它不适用于 DataGridTemplateColumns。
深入研究 DataGridColumn 类中的 OnPastingCellClipboardContent 方法,似乎 fe.GetBindingExpression(CellValueProperty) 返回 null 而不是所需的 BindingExpression。
谁能指出我正确的方向?
public virtual void OnPastingCellClipboardContent(object item, object cellContent)
{
BindingBase binding = ClipboardContentBinding;
if (binding != null)
{
// Raise the event to give a chance for external listeners to modify the cell content
// before it gets stored into the cell
if (PastingCellClipboardContent != null)
{
DataGridCellClipboardEventArgs args = new DataGridCellClipboardEventArgs(item, this, cellContent);
PastingCellClipboardContent(this, args);
cellContent = args.Content;
}
// Event handlers can cancel Paste of a cell by setting its content to null
if (cellContent != null)
{
FrameworkElement fe = new FrameworkElement();
fe.DataContext = item;
fe.SetBinding(CellValueProperty, binding);
fe.SetValue(CellValueProperty, cellContent);
BindingExpression be = fe.GetBindingExpression(CellValueProperty);
be.UpdateSource();
}
}
谢谢!