在 WPF DataGrid 中自定义 Ctrl+C 行为

NS.*_*.X. 5 wpf datagrid copy-paste

我正在使用 WPFDataGrid来显示名称/值对。SelectionUnit设置为FullRow因为它看起来不错,但是,当用户选择一行并按Ctrl+时C,他确实想要复制值文本,而不是默认行为(名称和值的串联)。在寻找解决方案时,我发现了CopyingRowClipboardContentevent,但MSDN 页面没有有关如何使用它的信息。还是我应该捕获我PreviewKeyDown自己?

Phi*_*hil 1

CopyingRowClipboardContent您可以使用 来修改事件处理程序中复制的数据DataGridRowClipboardEventArgs

反编译源码

public class DataGridRowClipboardEventArgs 
{
    /// <summary>
    /// This list should be used to modify, add or remove a cell
    /// content before it gets stored into the clipboard.
    /// </summary> 
    public List<DataGridClipboardCellContent> ClipboardRowContent
    { 
       ...
Run Code Online (Sandbox Code Playgroud)

例如,如果您有两列并且您只想要第一列,则可以像这样删除第二项:

private void grid_CopyingRowClipboardContent(
    object sender, DataGridRowClipboardEventArgs e)
{
    e.ClipboardRowContent.RemoveAt(1);
}
Run Code Online (Sandbox Code Playgroud)