【问题标题】:Set CopyingRowClipboardContent event dynamically for all DataGrids为所有 DataGrid 动态设置 CopyingRowClipboardContent 事件
【发布时间】:2019-07-15 02:39:57
【问题描述】:

当我在 DataGrid 中按 Ctrl + C 时,我希望能够复制一个单元格而不是一行,我发现我可以使用事件 CopyingRowClipboardContent 和处理程序来做到这一点:

Private Sub DataGrid_CopyingRowClipboardContent(ByVal sender As Object, ByVal e As DataGridRowClipboardEventArgs)
    Dim currentCell = e.ClipboardRowContent(dataGrid.CurrentCell.Column.DisplayIndex)
    e.ClipboardRowContent.Clear()
    e.ClipboardRowContent.Add(currentCell)
End Sub

我想将此行为全局分配给在运行时创建的任何 DataGrid(而不是返回我的代码并将处理程序一一添加到所有 DataGrid)。

我尝试将以下行添加到我的 <Window.Resources> 代码中:

<Style x:Key="DataGridCustomStyle" TargetType="{x:Type DataGrid}">
    <EventSetter Event="CopyingRowClipboardContent" Handler="Datagrid_CopyingRowClipboardContent" />
</Style>

但我收到一条错误消息,提示 CopyingRowClipboardContent 必须是 RoutedEvent 事件。

【问题讨论】:

  • 我面临同样的问题。你有没有让它工作?
  • @RafaGomez 不幸的是没有。

标签: wpf vb.net visual-studio-2010


【解决方案1】:

我在评论后再次阅读了您的问题,我认为我们不是在寻找相同的......

如果您只想选择一个单元格而不是整行,那么我认为您可以选择

<Style x:Key="DataGridCustomStyle" TargetType="{x:Type DataGrid}">
    <!--Copy only selected content without header-->
    <Setter Property="ClipboardCopyMode" Value="ExcludeHeader" />
    <!--Able to select many cells-->
    <Setter Property="SelectionMode" Value="Extended" />
    <!--Able to select cells or entire row/column-->
    <Setter Property="SelectionUnit" Value="CellOrRowHeader" />
</Style>

因此,如果用户只选择了一个单元格并按 Ctrl+C,则只有选定的单元格内容将被粘贴到剪贴板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2011-01-11
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多