Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Took me some time to answer this question, so here's how I did it to save someone else time:</p> <p>Function to copy the data to the clipboard, it also solves the problem of getting the rows in the right order in the resultant string:</p> <pre><code>void copy_selected() { if (listview.SelectedItems.Count != 0) { //where MyType is a custom datatype and the listview is bound to a //List&lt;MyType&gt; called original_list_bound_to_the_listview List&lt;MyType&gt; selected = new List&lt;MyType&gt;(); var sb = new StringBuilder(); foreach(MyType s in listview.SelectedItems) selected.Add(s); foreach(MyType s in original_list_bound_to_the_listview) if (selected.Contains(s)) sb.AppendLine(s.ToString());//or whatever format you want try { System.Windows.Clipboard.SetData(DataFormats.Text, sb.ToString()); } catch (COMException) { MessageBox.Show("Sorry, unable to copy surveys to the clipboard. Try again."); } } } </code></pre> <p>I still have occasional issues with the a COMException when I copy stuff to the clipboard, hence the try-catch. I seem to have solved this (in a very bad and lazy fashion) by sort of clearing the clipboard, see below.</p> <p>And to bind this to Ctrl + C</p> <pre><code>void add_copy_handle() { ExecutedRoutedEventHandler handler = (sender_, arg_) =&gt; { copy_selected(); }; var command = new RoutedCommand("Copy", typeof(GridView)); command.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control, "Copy")); listview.CommandBindings.Add(new CommandBinding(command, handler)); try { System.Windows.Clipboard.SetData(DataFormats.Text, ""); } catch (COMException) { } } </code></pre> <p>which is called from:</p> <pre><code>public MainWindow() { InitializeComponent(); add_copy_handle(); } </code></pre> <p>Obviously this is copied a lot from the link above, just simplified but I hope it's useful.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload