Paste Functionality for WPF DataGrid with DataGridTemplateColumns

Posted by zmang on Stack Overflow See other posts from Stack Overflow or by zmang
Published on 2010-04-12T22:16:29Z Indexed on 2010/04/12 23:22 UTC
Read the original article Hit count: 1727

Filed under:
|
|

Hi. I recently started using the WPF Datagrid with DataGridTemplateColumns containing the WPF AutoCompleteBox, but I'm finding trouble in implementing Clipboard.Paste functionality for these DataGridTemplateColumns.

I've managed to get Clipboard.Paste working with built-in DataGridColumns via Vishal's guide here, but it doesn't work with DataGridTemplateColumns.

Delving into the OnPastingCellClipboardContent method in the DataGridColumn class, it appears that fe.GetBindingExpression(CellValueProperty) is returning null rather than the required BindingExpression.

Can anyone point me to the right direction?

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();

    }

}

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpfdatagrid