WPF 列表框复制到剪贴板

发布于 2024-09-27 16:20:23 字数 93 浏览 2 评论 0原文

我正在尝试使用 CTRL+C 将标准 WPF ListBox 选定的项目(显示的)文本复制到剪贴板。有没有什么简单的方法可以实现这一点。如果它适用于应用程序中的所有列表框。

I am trying to copy a standard WPF ListBox selected Item (displayed) text to clipboard on CTRL+C. Is there any simple way to achieve this. If it is something that works for all the listboxes in the app.

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

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

发布评论

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

评论(1

勿忘初心 2024-10-04 16:20:23

由于您在 WPF 中,因此您可以尝试附加的行为
首先你需要一个像这样的类:

public static class ListBoxBehaviour
{
    public static readonly DependencyProperty AutoCopyProperty = DependencyProperty.RegisterAttached("AutoCopy",
        typeof(bool), typeof(ListBoxBehaviour), new UIPropertyMetadata(AutoCopyChanged));

    public static bool GetAutoCopy(DependencyObject obj_)
    {
        return (bool) obj_.GetValue(AutoCopyProperty);
    }

    public static void SetAutoCopy(DependencyObject obj_, bool value_)
    {
        obj_.SetValue(AutoCopyProperty, value_);
    }

    private static void AutoCopyChanged(DependencyObject obj_, DependencyPropertyChangedEventArgs e_)
    {
        var listBox = obj_ as ListBox;
        if (listBox != null)
        {
            if ((bool)e_.NewValue)
            {
                ExecutedRoutedEventHandler handler =
                    (sender_, arg_) =>
                    {
                        if (listBox.SelectedItem != null)
                        {
                            //Copy what ever your want here
                            Clipboard.SetDataObject(listBox.SelectedItem.ToString());
                        }
                    };

                var command = new RoutedCommand("Copy", typeof (ListBox));
                command.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control, "Copy"));
                listBox.CommandBindings.Add(new CommandBinding(command, handler));
            }
        }
    }
}

然后你就有了像这样的 XAML

<ListBox sample:ListBoxBehaviour.AutoCopy="True">
  <ListBox.Items>
    <ListBoxItem Content="a"/>
    <ListBoxItem Content="b"/>
  </ListBox.Items>
</ListBox>

更新:对于最简单的情况,您可以通过以下方式访问文本:

private static string GetListBoxItemText(ListBox listBox_, object item_)
{
  var listBoxItem = listBox_.ItemContainerGenerator.ContainerFromItem(item_)
                    as ListBoxItem;
  if (listBoxItem != null)
  {
    var textBlock = FindChild<TextBlock>(listBoxItem);
    if (textBlock != null)
    {
      return textBlock.Text;
    }
  }
  return null;
}

GetListBoxItemText(myListbox, myListbox.SelectedItem)
FindChild<T> is a function to find a child of type T of a DependencyObject

但是就像 ListBoxItem 可以绑定到对象一样,ItemTemplate 也可能不同,因此您不能在实际项目中依赖它。

As you're in WPF so you could try the attached behaviours

First you need a class like this:

public static class ListBoxBehaviour
{
    public static readonly DependencyProperty AutoCopyProperty = DependencyProperty.RegisterAttached("AutoCopy",
        typeof(bool), typeof(ListBoxBehaviour), new UIPropertyMetadata(AutoCopyChanged));

    public static bool GetAutoCopy(DependencyObject obj_)
    {
        return (bool) obj_.GetValue(AutoCopyProperty);
    }

    public static void SetAutoCopy(DependencyObject obj_, bool value_)
    {
        obj_.SetValue(AutoCopyProperty, value_);
    }

    private static void AutoCopyChanged(DependencyObject obj_, DependencyPropertyChangedEventArgs e_)
    {
        var listBox = obj_ as ListBox;
        if (listBox != null)
        {
            if ((bool)e_.NewValue)
            {
                ExecutedRoutedEventHandler handler =
                    (sender_, arg_) =>
                    {
                        if (listBox.SelectedItem != null)
                        {
                            //Copy what ever your want here
                            Clipboard.SetDataObject(listBox.SelectedItem.ToString());
                        }
                    };

                var command = new RoutedCommand("Copy", typeof (ListBox));
                command.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control, "Copy"));
                listBox.CommandBindings.Add(new CommandBinding(command, handler));
            }
        }
    }
}



Then you have the XAML like this

<ListBox sample:ListBoxBehaviour.AutoCopy="True">
  <ListBox.Items>
    <ListBoxItem Content="a"/>
    <ListBoxItem Content="b"/>
  </ListBox.Items>
</ListBox>



Updates: For the simplest case, you can access the text in the below way:

private static string GetListBoxItemText(ListBox listBox_, object item_)
{
  var listBoxItem = listBox_.ItemContainerGenerator.ContainerFromItem(item_)
                    as ListBoxItem;
  if (listBoxItem != null)
  {
    var textBlock = FindChild<TextBlock>(listBoxItem);
    if (textBlock != null)
    {
      return textBlock.Text;
    }
  }
  return null;
}

GetListBoxItemText(myListbox, myListbox.SelectedItem)
FindChild<T> is a function to find a child of type T of a DependencyObject

But just like the ListBoxItem could be bound to object, the ItemTemplate could be different as well, so you can't rely on it in real projects.

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