I have a DataGrid
. But I want to get focused cell value in CopyingRowClipboardContent
event. But e.ClipboardRowContent
returns me all selected cells values because of the SelectionUnit
. And i must not change selection unit of datagrid. For solving the problem i need to get focused cell column number. Then I will remove all column values from clipboarcContent
. How can i get focused cell in CopyingRowClipboardContent
event?
Improved version of Farhad's answer
private void DataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
var currentCell = e.ClipboardRowContent[ dataGrid.CurrentCell.Column.DisplayIndex];
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.Add( currentCell );
}
您还可以使用以下代码来控制剪贴板内容。
Clipboard.SetText("some value");
I find the solution. First of all I have needed column number of the focused cell. I have managed to get it with this code:
DataGridResults.CurrentCell.Column.DisplayIndex;
Then in CopyingRowClipboardContent
event, I must delete all other column values.
private void DataGridResults_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
int y = 0;
for (int i = 0; i < e.EndColumnDisplayIndex; i++)
{
if (i != DataGridResults.CurrentCell.Column.DisplayIndex)
{
e.ClipboardRowContent.RemoveAt(i - y);
y++;
}
}
}
I found that this solution worked for me on all DataGrids; even ones that had hidden columns.
// Clipboard Row content only includes entries for visible cells
// Figure out the actual column we are looking for (taking into account hidden columns)
int columnIndex = dataGrid.CurrentCell.Column.DisplayIndex;
var column = dataGrid.Columns[columnIndex];
// Find the associated column we're interested in from the clipboard row content
var cellContent = clipboardRowContent.Where(item => item.Column == column).First();
clipboardRowContent.Clear();
clipboardRowContent.Add(cellContent);
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.