How to handle Copy and Paste of Read Only in WPF GridTreeControl?
The GridTreeControl provides support for the clipboard copy and paste operations and not for ReadOnly Grid and columns. You can achieve this by changing the read only value of the Grid and Gridcell for the readonly column at runtime by using the ClipBoardPaste and ClipBoardPasted events in the GridTreeControl.
Refer to the following code example to wire the ClipboardPaste and ClipboardPasted events in the GridTreeControl.
C#
treeGrid.Model.ClipboardPaste += Model_ClipboardPaste; treeGrid.Model.ClipboardPasted += Model_ClipboardPasted; |
You need to set ReadOnly as false for both Grid and Gridcell to allow the paste operation in the ClipBoardPaste event when the Grid and Gridcell have ReadOnly as true. Please refer to the following code example that illustrates this.
C#
void Model_ClipboardPaste(object sender, GridCutPasteEventArgs e) { //Get and check the Row index by using RangeList. for (int row = e.RangeList.ActiveRange.Top; row <= e.RangeList.ActiveRange.Bottom; row++) { //Check the condition based on the total column count. for (int col = 0; col < treeGrid.Model.ColumnCount; col++) { //Get the column index by using ResolveIndexToColumnIndex. var colindex = treeGrid.Model.TreeGrid.ResolveIndexToColumnIndex(col); //Check if the column as ReadOnly. if (colindex >= 0 && treeGrid.Columns[colindex].StyleInfo.ReadOnly) { //Set ReadOnly as false for a GridCell. treeGrid.Model[row, col].ReadOnly = false; } } } //Set ReadOnly as false for Grid. treeGrid.ReadOnly = false; } |
The ClipboardPasted event fires after pasting the data in the GridTreeControl and you can set an actual value, True, to ReadOnly property of the Grid and GridCell. Refer to the following code example that illustrates this.
C#
void Model_ClipboardPasted(object sender, GridCutPasteEventArgs e) { //Get and check the Row index by using RangeList. for (int row = e.RangeList.ActiveRange.Top; row <= e.RangeList.ActiveRange.Bottom; row++) { //Check the condition based on the total column count for (int col = 0; col < treeGrid.Model.ColumnCount; col++) { //Get the column index by using ResolveIndexToColumnIndex var colindex = treeGrid.Model.TreeGrid.ResolveIndexToColumnIndex(col); //Check if the column as ReadOnly if (colindex >= 0 && !treeGrid.Columns[colindex].StyleInfo.ReadOnly) { //Set ReadOnly as true for GridCell treeGrid.Model[row, col].ReadOnly = true; } } } //Set ReadOnly as True for Grid treeGrid.ReadOnly = true; } |
The following screenshot displays the row to be pasted.
Figure 1: Row to Copy
The following screenshot displays the row pasted.
Figure 2: The copied Row pasted
You can refer to the following sample link to handle the copy and paste operations for the ReadOnly Grid and columns.
Sample Link: CopyandPasteDemo_GridTreeControl
Conclusion
I hope you enjoyed learning about how to handle Copy and Paste of Read Only in WPF GridTreeControl.
You can refer to our WPF TreeGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF TreeGrid documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!