Paste functionality for WPF DataGrid using DataGridTemplateColumns

I recently started using WPF Datagrid with a DataGridTemplateColumns containing WPF AutoCompleteBox, but I found problems implementing the Clipboard.Paste function for these DataGridTemplateColumns.

I managed to load Clipboard.Paste with embedded DataGridColumns through the Vishal guide here , but this does not work with DataGridTemplateColumns.

Returning to the OnPastingCellClipboardContent method in the DataGridColumn class, it seems that fe.GetBindingExpression (CellValueProperty) returns zero, not the required BindingExpression.

Can someone point me in 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();

    }

}

Thank!

+3
4

ClipboardContentBinding TwoWay, , .

GetBindingExpression - null ( ClipboardContentBinding) UpdateSource .

, , PropertyChanged, , , , DataTemplate.

+1

, DataGridTemplateColumns . . ( ) . , .

, . DataGridTextColumn ( , ) GenerateElement GenerateEditingElement.

, , .

0

See this link for information on creating a custom column.

How to set values ​​to custom DataGridBound column in WPF

0
source

Use ClipboardContentBindingas shown:

<DataGridTemplateColumn 
    Header="First Name" 
    SortMemberPath="FirstName" 
    ClipboardContentBinding="{Binding FirstName}" 
    >
    <DatGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding FirstName}" />
        </DataTemplate>
    </DatGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        ...
    </DataGridTemplateColumn>
</DataGridTemplateColumn>

Taken from here .

0
source

Source: https://habr.com/ru/post/1740838/


All Articles