views:

910

answers:

2

hello, how can I copy the selected items in a WPF's ListView with binding to db fields to the Clipboard?

thank you Cristian

+1  A: 

I would think you would have to monitor for SelectionChanged events and then format the items in a particular text format and then utilize the Clipboard.SetText method to set the items into the clipboard.

http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        foreach (var item in e.AddedItems.OfType<ListViewItem>())
        {
                Clipboard.SetText(item.ToString());
        }
}
Adam Driscoll
This answer seems much cleaner. Given this basic format, you can even get the underlying data and format that directly.
Chris Ridenour
+2  A: 

Since you want what is being displayed as opposed to the data on your class's properties you will need to grab the data from the controls directly.

        var sb = new StringBuilder();
        foreach(var item in this.listview1.SelectedItems)
        {
            var lvi = this.listview1.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem;
            var cell = this.GetVisualChild<ContentPresenter>(lvi);
            var txt = cell.ContentTemplate.FindName("txtCodCli", cell) as TextBlock;
            sb.Append(txt.Text);
            //TODO: grab the other column's templated controls here & append text
        }
        System.Windows.Clipboard.SetData(DataFormats.Text, sb.ToString());

This assumes that in your XAML you have

<TextBlock x:Name="txtCodCli" TextAlignment="Left" Text="{Binding Path=VFT_CLI_CODICE}" />

"Where GetVisualChild T is

    public T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
Software.Developer
hellom this is not working: here a piece of the XAML:<ListView.View> <GridView> <GridViewColumn Width="80"> <GridViewColumnHeader Content="Cod. Cli."></GridViewColumnHeader> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <TextBlock TextAlignment="Left" Text="{Binding Path=VFT_CLI_CODICE}" /> </StackPanel>
Cristian
foreach(var item in this.listview1.SelectedItems){ var lvi = this.listview1.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem; var myBoundItem = lvi.Content as MyClass; sb.AppendLine(myBoundItem.VFT_CLI_CODICE);}System.Windows.Clipboard.SetData(DataFormats.Text, sb.ToString());Where "MyClass" is whatever object has that VFT_CLI_CODICE property.
Software.Developer
that is working, but I need to obtain programmatically the columns visible on the ListView, because not all the field from "MyClass" are displayed. and to some field I'm applying some converter, and want the data copied to the clipboard, with the format applied!
Cristian