<i id='FegAe'><tr id='FegAe'><dt id='FegAe'><q id='FegAe'><span id='FegAe'><b id='FegAe'><form id='FegAe'><ins id='FegAe'></ins><ul id='FegAe'></ul><sub id='FegAe'></sub></form><legend id='FegAe'></legend><bdo id='FegAe'><pre id='FegAe'><center id='FegAe'></center></pre></bdo></b><th id='FegAe'></th></span></q></dt></tr></i><div id='FegAe'><tfoot id='FegAe'></tfoot><dl id='FegAe'><fieldset id='FegAe'></fieldset></dl></div>

      • <bdo id='FegAe'></bdo><ul id='FegAe'></ul>
    1. <tfoot id='FegAe'></tfoot>
      <legend id='FegAe'><style id='FegAe'><dir id='FegAe'><q id='FegAe'></q></dir></style></legend>

      <small id='FegAe'></small><noframes id='FegAe'>

        WPF 列表框复制到剪贴板

        WPF listbox copy to clipboard(WPF 列表框复制到剪贴板)
        • <tfoot id='vZ4dF'></tfoot>
            <bdo id='vZ4dF'></bdo><ul id='vZ4dF'></ul>
            <legend id='vZ4dF'><style id='vZ4dF'><dir id='vZ4dF'><q id='vZ4dF'></q></dir></style></legend>

                  <tbody id='vZ4dF'></tbody>

                1. <small id='vZ4dF'></small><noframes id='vZ4dF'>

                  <i id='vZ4dF'><tr id='vZ4dF'><dt id='vZ4dF'><q id='vZ4dF'><span id='vZ4dF'><b id='vZ4dF'><form id='vZ4dF'><ins id='vZ4dF'></ins><ul id='vZ4dF'></ul><sub id='vZ4dF'></sub></form><legend id='vZ4dF'></legend><bdo id='vZ4dF'><pre id='vZ4dF'><center id='vZ4dF'></center></pre></bdo></b><th id='vZ4dF'></th></span></q></dt></tr></i><div id='vZ4dF'><tfoot id='vZ4dF'></tfoot><dl id='vZ4dF'><fieldset id='vZ4dF'></fieldset></dl></div>
                2. 本文介绍了WPF 列表框复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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 int he app.. that would be great.

                  提前致谢.

                  推荐答案

                  因为你在 WPF 中所以你可以尝试附加的行为
                  首先你需要一个这样的类:

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


                  然后你就有了这样的 XAML


                  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
                  

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

                  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.

                  这篇关于WPF 列表框复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

                    • <legend id='ZBtUr'><style id='ZBtUr'><dir id='ZBtUr'><q id='ZBtUr'></q></dir></style></legend>
                        <tbody id='ZBtUr'></tbody>
                        <tfoot id='ZBtUr'></tfoot>
                        <i id='ZBtUr'><tr id='ZBtUr'><dt id='ZBtUr'><q id='ZBtUr'><span id='ZBtUr'><b id='ZBtUr'><form id='ZBtUr'><ins id='ZBtUr'></ins><ul id='ZBtUr'></ul><sub id='ZBtUr'></sub></form><legend id='ZBtUr'></legend><bdo id='ZBtUr'><pre id='ZBtUr'><center id='ZBtUr'></center></pre></bdo></b><th id='ZBtUr'></th></span></q></dt></tr></i><div id='ZBtUr'><tfoot id='ZBtUr'></tfoot><dl id='ZBtUr'><fieldset id='ZBtUr'></fieldset></dl></div>

                          <small id='ZBtUr'></small><noframes id='ZBtUr'>

                            <bdo id='ZBtUr'></bdo><ul id='ZBtUr'></ul>