简体   繁体   English

在 WPF C# 中将 ListView 的项目复制到剪贴板

[英]Copy a ListView’s Items to the Clipboard in WPF C#

how can I copy the selected items in a WPF's ListView with binding to db fields to the Clipboard?如何将 WPF 的 ListView 中选定的项目与绑定到 db 字段的项目复制到剪贴板?

thank you Cristian谢谢克里斯蒂安

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这假设在您的 XAML 中您有

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

"Where GetVisualChild T is “GetVisualChild T 在哪里

    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;
    }

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.我认为您必须监视 SelectionChanged 事件,然后以特定文本格式格式化项目,然后利用 Clipboard.SetText 方法将项目设置到剪贴板中。

http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx 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());
        }
}

I solved it this way.我是这样解决的。 I have added Ctrl + C event listener and then added logic to copy to clipboard我添加了Ctrl + C事件侦听器,然后添加了复制到剪贴板的逻辑

XAML XAML

<Window 
    ....
        Loaded="Window_Loaded"
        ...>

in code在代码中

private void WindowKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
            {
                var sb = new StringBuilder();
                var selectedItems = LVLog.SelectedItems;

                foreach(var item in selectedItems)
                {
                    sb.Append($"{item.ToString()}\n");
                }

                Clipboard.SetDataObject(sb.ToString());
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM