WPF ListView的GridView复制到剪贴板

发布于 2024-11-14 04:20:03 字数 616 浏览 5 评论 0原文

我有一个如下所示的 GridView:

    <ListView ItemsSource="{Binding Path=Foo, Mode=OneWay}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Full name" 
                                DisplayMemberBinding="{Binding Path=FullName}" />

我所希望的是,当按 Ctrl + C 时,所有项目(或选定的项目)都会复制到剪贴板。目前还不是。我正在使用 WPF 3.0。

WPF 列表框复制到剪贴板 部分回答,但我需要的似乎更简单,我想有也是一个更简单的解决方案。

PS:这个GridView不支持内置的列排序等。如果您知道更好的免费控件并且支持复制,请随时建议它作为解决方案。

I've a GridView like follow:

    <ListView ItemsSource="{Binding Path=Foo, Mode=OneWay}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Full name" 
                                DisplayMemberBinding="{Binding Path=FullName}" />

All I wish is that when pressing Ctrl + C all items (or selected items) are copied to the clipboard. Currently it's not. I'm using WPF 3.0.

Partially answered by WPF listbox copy to clipboard but what I need seem simpler and I guess has also a simpler solution.

PS: This GridView doesn't support built-in column sorting and so on. If you know a better control that's free and support copying feel free to suggest it as a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2024-11-21 04:20:03

我花了一些时间来回答这个问题,所以我是这样做的,以节省其他人的时间:

将数据复制到剪贴板的函数,它还解决了在结果字符串中以正确的顺序获取行的问题:

void copy_selected()
{
    if (listview.SelectedItems.Count != 0)
    {
        //where MyType is a custom datatype and the listview is bound to a 
        //List<MyType> called original_list_bound_to_the_listview
        List<MyType> selected = new List<MyType>();
        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.");
        }
    }
}

我仍然当我将内容复制到剪贴板时,偶尔会出现 COMException 问题,因此需要 try-catch。我似乎已经通过清除剪贴板解决了这个问题(以一种非常糟糕和懒惰的方式),见下文。

并将其绑定到

void add_copy_handle()
{
    ExecutedRoutedEventHandler handler = (sender_, arg_) => { 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)
    { }
}

从以下位置调用的 Ctrl + C:

public MainWindow()
{
    InitializeComponent();
    add_copy_handle();
}

显然,这是从上面的链接复制的很多内容,只是进行了简化,但我希望它有用。

Took me some time to answer this question, so here's how I did it to save someone else time:

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:

void copy_selected()
{
    if (listview.SelectedItems.Count != 0)
    {
        //where MyType is a custom datatype and the listview is bound to a 
        //List<MyType> called original_list_bound_to_the_listview
        List<MyType> selected = new List<MyType>();
        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.");
        }
    }
}

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.

And to bind this to Ctrl + C

void add_copy_handle()
{
    ExecutedRoutedEventHandler handler = (sender_, arg_) => { 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)
    { }
}

which is called from:

public MainWindow()
{
    InitializeComponent();
    add_copy_handle();
}

Obviously this is copied a lot from the link above, just simplified but I hope it's useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文