none
How to copy read-only DataGrid cell context to ClipBoard

    Question

  • im using .NET 3.5, Microsoft.Windows.Controls.DataGrid. My DataGrid is ReadOnly. If I click on a Cell the entire row becomes active but only one cell is selected (selected cell has border around it). What I need to do know is to use Control-C to copy the cell content to clipboard. Right now it copys the entire row to clupboard. I need to drag & select some part of the text in the cell and copy-it but I can not do that. Please advice...
    Monday, October 03, 2011 1:00 PM

Answers

  • Use the Key event on the selected cell to get the selected content into the clipboard, please try my code:

        <tk:DataGrid ItemsSource="{Binding}" IsReadOnly="True">
          <tk:DataGrid.CellStyle>
            <Style TargetType="{x:Type tk:DataGridCell}">
              <EventSetter Event="PreviewKeyDown" Handler="DataGridCell_PreviewKeyDown"/>
            </Style>
          </tk:DataGrid.CellStyle>
        </tk:DataGrid>
    

    C#:

        bool IsControlPressed = false;
        private void DataGridCell_PreviewKeyDown(object sender, KeyEventArgs e)
        {
          DataGridCell cell = sender as DataGridCell;
          if (cell != null && cell.IsSelected == true)
          {
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
              IsControlPressed = true;
            }
            if (IsControlPressed && e.Key == Key.C)
            {
              if (cell.Content is TextBlock)
                Clipboard.SetText((cell.Content as TextBlock).Text);
              IsControlPressed = false;
              e.Handled = true;
            }
          }
        }
    

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by GaiusBaltar Tuesday, October 04, 2011 10:26 AM
    Tuesday, October 04, 2011 6:21 AM
    Moderator

All replies

  • Use the Key event on the selected cell to get the selected content into the clipboard, please try my code:

        <tk:DataGrid ItemsSource="{Binding}" IsReadOnly="True">
          <tk:DataGrid.CellStyle>
            <Style TargetType="{x:Type tk:DataGridCell}">
              <EventSetter Event="PreviewKeyDown" Handler="DataGridCell_PreviewKeyDown"/>
            </Style>
          </tk:DataGrid.CellStyle>
        </tk:DataGrid>
    

    C#:

        bool IsControlPressed = false;
        private void DataGridCell_PreviewKeyDown(object sender, KeyEventArgs e)
        {
          DataGridCell cell = sender as DataGridCell;
          if (cell != null && cell.IsSelected == true)
          {
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
              IsControlPressed = true;
            }
            if (IsControlPressed && e.Key == Key.C)
            {
              if (cell.Content is TextBlock)
                Clipboard.SetText((cell.Content as TextBlock).Text);
              IsControlPressed = false;
              e.Handled = true;
            }
          }
        }
    

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by GaiusBaltar Tuesday, October 04, 2011 10:26 AM
    Tuesday, October 04, 2011 6:21 AM
    Moderator
  • Is is possible to add this feature on resource.xml file? So if I all my datagrid-s have this or do I have to add this to all my datagrids in my app
    Tuesday, October 04, 2011 12:50 PM
  • Sure, you could declare a style without the key in the general resource dictionary for DataGridCell. Then specify the EventSetter in it.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 05, 2011 5:04 AM
    Moderator