最近、WPF AutoCompleteBox を含む DataGridTemplateColumns で WPF Datagrid を使い始めましたが、これらの DataGridTemplateColumns に Clipboard.Paste 機能を実装する際に問題が発生しています。
Vishal のガイドhereを介して、Clipboard.Paste を組み込みの DataGridColumns で動作させることができましたが、DataGridTemplateColumns では動作しません。
DataGridColumn クラスの OnPastingCellClipboardContent メソッドを調べてみると、必要な BindingExpression ではなく、fe.GetBindingExpression(CellValueProperty) が null を返しているようです。
誰かが私を正しい方向に向けることができますか?
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();
}
}
ありがとう!