Select arbitrary text in a WPF datagrid row or cell and copy it

Scenario:

I have an ObservableCollection containing log events. A log event consists out of eg time, date, severity and a message. I am using a WPF datagrid with data binding to display the content of the ObservableCollection.

Problem:

As an end-user I would like to mark/select arbitrary amounts of text in one row and copy it to the clipboard. For an example I would like to select single words in the message column or the whole row like it is possible in excel or html rendered tables.

Already tried:

I have tried to change the template for the cells to a readonly textbox which makes it possible to mark the whole cell or single words but then selecting and copying the whole row does not work anymore. I have also considered using the "FlowDocument" control but a major feature of the UI is that new log messages are added without repainting the whole control. As far as I know this is not possible with FlowDocument which also lacks data binding support.

Question:

How to display a list of (for an example) log events in WPF where one can select and copy arbitrary text portions like it would be possible in Excel or a rendered html table?


Could try this property:

<DataGrid SelectionUnit="Cell">

ADDED:

<DataGrid ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectionUnit="Cell" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Mode=OneWay}"/>
        <DataGridTextColumn Binding="{Binding Baseline}"/>
        <DataGridTextColumn Binding="{Binding LineSpacing}" Width="*"/>
    </DataGrid.Columns>
</DataGrid>

To select an entire row at time and copy to clipboard

<DataGrid ... SelectionUnit="FullRow" CopyingRowClipboardContent="dataPaths_CopyingRowClipboardContent">

calls

private void dataPaths_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
   IList<DataGridCellInfo> cells = (sender as DataGrid).SelectedCells;

   foreach (DataGridCellInfo cell in cells)
       e.ClipboardRowContent.Add(new DataGridClipboardCellContent(e.Item, cell.Column, null));

   //or write out to a log
   foreach (var row in e.ClipboardRowContent)
      Console.WriteLine(row.Item.toString()); 
}
链接地址: http://www.djcxy.com/p/64732.html

上一篇: 使用WPF在表格中分组并显示一组事件

下一篇: 在WPF数据网格行或单元格中选择任意文本并复制它