简体   繁体   中英

Copy to Clipboard the entire Selected Row data in TextBox from WPF DataGrid

Im using a WPF Datagrid and placed a textbox in all the cell templates.means my entire Row consists of all the textbox with data binded to that.Now when i select any row from the datagrid and hit ctrl+c,i want to copy entire row data to clipboard.

<DataGridTemplateColumn Header="Text" >
<DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
             <TextBox Text="{Binding Path=SAMPLETEXT}" />
       </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

Tried using the below code but it is throwing empty data ie not copying anything.I think this is because my complete Row consists of textbox.

private void DataGrid_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Key == Key.C && (e.SystemKey == Key.LeftCtrl || e.SystemKey == Key.RightCtrl))
    {

        ApplicationCommands.Copy.Execute(null, dataGridTest);

    }
}

Please suggest me any better approach. Thanks for reading.


UPDATE Adding below line of code to DataGridTemplateColumn worked for me.

ClipboardContentBinding="{Binding SampleText}"

You will have to pro-grammatically set values to Clipboard using Clipboard class:

Clipboard.SetText("your data");

Extract data from the selected row, combine it in a string variable and assign that variable to the Clipboard .

I know this is an older post, but this solution is posted for completeness and is missing the use of a suited DataGrid event method signature associated with the DataGridRowClipboardEventArgs.

Clipboard.SetText can be flaky, not grabbing/setting the clipboard all the time.

Set "FullRow" at the SelectionUnit mode for dataGrid called myDataGrid

<DataGrid x:Name="myDataGrid" SelectionUnit="FullRow"></DataGrid>

We have a method myDataGrid_CopyingRowClipboardContent that gets called for each row in the dataGrid to copy its contents to the clipboard. For example,for a datagrid with 10 rows this is called 10 times.

public int clipboardcalledcnt { get; set; } //CopyingRowClipboardContent invoked count
private void myDataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
    PathInfo cellpath = new PathInfo(); //a custom class to hold path info
    string path = string.Empty;

DataGrid dgdataPaths = (DataGrid)sender;
int rowcnt = dgdataPaths.SelectedItems.Count;

cellpath = (PathInfo)e.Item;

path = "Row #"+ clipboardcalledcnt +" Len="+ cellpath.Length.ToString() + ", path=" + cellpath.Path;

e.ClipboardRowContent.Clear();

if (clipboardcalledcnt == 0) //add header to clipboard paste
    e.ClipboardRowContent.Add(new DataGridClipboardCellContent("", null, "--- Clipboard Paste ---\t\t\n")); // \t cell divider, repeat (number of cells - 1)

clipboardcalledcnt++;
e.ClipboardRowContent.Add(new DataGridClipboardCellContent(path, null, path));

if (clipboardcalledcnt == rowcnt)
    clipboardcalledcnt = 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM