This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi There, I have an BindingList of Data object that is bound to the DataGrid. We tried to override the "Ctrl C" and "Ctrl V" behavior: Copy the whole selected data object into clipboard. But in the end the content is the string shows on the Data Grid not the object underneath. We set the selection type for the data grid is SelectionType="RowAndCell" But we don't want it to override the cell editing feature: Highlight a cell, ctrl c will just copy the cell value and ctrl v will just the paste the content to a highlighted cell. I have achieved this with context menu "Copy" and "Paste", but the user wants to use Ctrl C and Ctrl V. If you need more info, please let me know. Thanks, Gordon |
|
|
override OnPreviewKeyDown function seemed do the trick. I presume the copy past is down in the PreviewKeyDown events. Thanks, Gordon |
|
|
Hello Gordon The best way to provide Copy and Paste functionality would be to use the built-in WPF commands. Below is a code example that shows you how to do this with the Copy command. dg is a DataGrid instance. CopyExecuted and CopyCanExecute are methods that conform to the usual WPF command delegates.
You will also first want to disable our built-in Copy functionality so that it does not interfere with yours. This would be done by iterating the DataGrid.CommandBindings collection and removing the Copy command binding. If you would like to follow this approach, let me know if you have any questions. If your current approach works well for you though, feel free to continue using that. I personally like to avoid using "preview" events though as they can be easy to break existing behaviours. -Jason Fauchelle |
|
|
Thanks, Jason. I finally got some time to try your way and it worked. This way is more elegant and I will use your way. One small problem, When there is no records on the datagrid (but the BindingList is not null), the the user has to click on the column header in order for the Ctrl+V work. if the user click on the white empty space on the datagrid, the ctrl+v will not work. (I checked the OnPreviewKeyDown is not called.) Thanks, Gordon |
|
|
Hello Gordon The way that WPF handles the focus state can be odd sometimes. I would expect that clicking anywhere on the DataGrid would cause it to gain focus and allow the key-bound commands to trigger. I could listen to mouse clicks to cause the DataGrid to gain focus, but since the DataGrid is composed of many individual focusable parts (the cells), doing this could easily cause cells to never reach edit mode because the DataGrid may steal the focus state. For now, I will avoid trying to resolve this so close to your release date. But you could give it a go at your end. -Jason Fauchelle |
|
|
I fixed on my end. Thanks Gordon |
|