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

5
我正在使用 WPF DataGrid 显示名称-值对。由于它看起来很好,因此将 SelectionUnit 设置为 FullRow,但是,当用户选择一行并按下 Ctrl+C 时,他真正想要复制的是值文本,而不是默认行为,即名称和值的串联。在寻找解决方案时,我发现了 CopyingRowClipboardContent 事件,但是 MSDN 页面 上没有关于如何使用它的信息。或者,我应该自己捕获 PreviewKeyDown
2个回答

1

您可以使用 DataGridRowClipboardEventArgsCopyingRowClipboardContent 事件处理程序中修改复制的数据。

反编译的源代码

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
    { 
       ...

例如,如果您有两列数据,但只需要第一列,您可以像这样删除第二个项目:
private void grid_CopyingRowClipboardContent(
    object sender, DataGridRowClipboardEventArgs e)
{
    e.ClipboardRowContent.RemoveAt(1);
}

这只是覆盖了每一行复制的文本,我来这里是为了寻找如何覆盖实际的命令绑定,以便永远不调用数据网格的实现。 - Alex Hope O'Connor

0

也许这就是你要找的

ClipboardCopyMode = DataGridClipboardCopyMode.ExcludeHeader;


不,那是要排除列标题,我想要排除行中的某些单元格。 - NS.X.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接