[英]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. 我使用WPF Datagrid并在所有单元格模板中放置了一个文本框。意味着我的整个行由绑定了数据的所有文本框组成。现在当我从数据网格中选择任何行并按ctrl + c时,我想复制整个行数据到剪贴板。
<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. 更新将下面的代码行添加到DataGridTemplateColumn对我有用。
ClipboardContentBinding="{Binding SampleText}"
You will have to pro-grammatically set values to Clipboard
using Clipboard
class: 您将必须使用
Clipboard
类在语法上将值设置为Clipboard
:
Clipboard.SetText("your data");
Extract data from the selected row, combine it in a string
variable and assign that variable to the Clipboard
. 从选定的行中提取数据,将其组合为
string
变量,然后将该变量分配给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. 我知道这是一篇较旧的文章,但是此解决方案出于完整性目的而发布,并且缺少与DataGridRowClipboardEventArgs关联的合适的DataGrid事件方法签名的使用。
Clipboard.SetText can be flaky, not grabbing/setting the clipboard all the time. Clipboard.SetText可能是片状的,并非一直都在抓取/设置剪贴板。
Set "FullRow" at the SelectionUnit mode for dataGrid called myDataGrid 在SelectionUnit模式下为名为myDataGrid的dataGrid设置“ FullRow”
<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. 我们有一个方法myDataGrid_CopyingRowClipboardContent,该方法将为dataGrid中的每一行调用以将其内容复制到剪贴板。 For example,for a datagrid with 10 rows this is called 10 times.
例如,对于具有10行的数据网格,这称为10次。
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;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.