简体   繁体   English

如何通过上下文菜单中的复制命令复制选定的列表视图项目

[英]How to copy selected listview item by copy command in the context menu

I've a listview and I want to copy the selected item by selecting the copy command in the context menu. 我有一个列表视图,我想通过在上下文菜单中选择复制命令来复制所选项目。 I'm able to create a context menu with the copy command as shown in the image below: 我可以使用copy命令创建上下文菜单,如下图所示:

在此处输入图片说明

The problem is when I select a row item and right click it, the context menu wouldn't show and I cannot select the copy command to copy the selected item. 问题是当我选择一个行项目并右键单击它时,上下文菜单不会显示,并且我无法选择复制命令来复制所选项目。 The context menu will show when I right click at the empty space in the list view. 当我右键单击列表视图中的空白区域时,将显示上下文菜单。

在此处输入图片说明

Here's the code: 这是代码:

.xaml 的.xaml

<ListView x: Name = "DebugLogLb" BorderBrush = "{x:Null}" SelectionMode = "Extended" MouseRightButtonDown = "DebugLogLb_MouseRightButtonDown">
  <ListViewItem x: Name = "DebugLogItem">
    <ListViewItem.ContextMenu>
      <ContextMenu x: Name = "CommandMenu">
        <MenuItem Header = "Clear log" Click = "CommandMenuItem_Click"/>
        <MenuItem Header = "Copy" Command = "Copy" CommandTarget = "{Binding ElementName=DebugLogItem}"/>       
      </ContextMenu>
    </ListViewItem.ContextMenu>
  </ListViewItem>
</ListView >

.xaml.cs: .xaml.cs:

 private void DebugLogLb_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
 {
   ContextMenu contextMenu = this.FindName("CommandMenu") as ContextMenu;
   contextMenu.PlacementTarget = sender as ListViewItem;
   contextMenu.IsOpen = true;
 }

 private void CommandMenuItem_Click(object sender, RoutedEventArgs e) 
 {
   MenuItem menuItem = (MenuItem) e.OriginalSource;

   switch (menuItem.Header.ToString())
   {
     case "Clear log":
       DebugLogLb.Items.Clear();
       break;
     default:
       break;
   }
 }

Something like this will let you use the ContextMenu with its MenuItems : 这样的事情将使您可以将ContextMenu及其MenuItems一起使用:

       <ListView x:Name = "DebugLogLb" BorderBrush = "{x:Null}" SelectionMode = "Extended" ItemsSource="{Binding items}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}">
                    <TextBlock.ContextMenu >
                        <ContextMenu x:Name = "CommandMenu">
                            <MenuItem Header = "Clear log" />
                            <MenuItem Header = "Copy" Command = "Copy" CommandTarget = "{Binding ElementName=DebugLogItem}"/>
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView >

So, define it inside the ItemTemplate in order to be able to use it when selecting each item. 因此,在ItemTemplate内部定义它,以便在选择每个项目时能够使用它。

The items collection is defined in code behind : items集合在后面的代码中定义:

    public ObservableCollection<string> items { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        items = new ObservableCollection<string>();
        items.Add("first item");
        items.Add("second item");
    }

Nothing fancy, just for the sake of example. 只是为了举例,没有什么幻想。

One more thing, if you still want it available when clicking on the white space, add a ContextMenu on the ListView too: 还有一件事,如果您仍然希望在单击空白时仍可用,请在ListView上也添加一个ContextMenu:

          <ListView.ContextMenu>
            <ContextMenu x:Name = "CommandMenu">
                <MenuItem Header = "Clear log" />
                <MenuItem Header = "Copy" Command = "Copy" CommandTarget = "{Binding ElementName=DebugLogItem}"/>
            </ContextMenu>
        </ListView.ContextMenu>

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

相关问题 在列表视图的上下文菜单中实现复制和粘贴 - implementing copy and paste in context menu of a listview 将上下文菜单 header 绑定到 ListView 的选定项 - Binding a context menu header to the selected item of a ListView 如何将所选上下文菜单项的索引传递给绑定命令? - How to pass an index of a selected context menu item to a bound command? WPF上下文菜单,复制菜单项显示为灰色 - WPF Context Menu, Copy Menu Item is grayed out 如何将自己的项目添加到文本框的默认上下文菜单(剪切,复制,粘贴, <my item> )。 - How can I add my own item to the default context menu of a textbox (cut, copy, paste, <my item>). 使用上下文菜单后获取选定的 ListView 项目 - Get selected ListView item after using Context Menu 如何为listView中的每个项目实现上下文菜单 - How to implement a context menu for each item in a listView 如何通过打开上下文操作菜单从ListView中获取所选项目? (Xamarin.Forms) - How do I get the selected item from a ListView by opening the context actions menu? (Xamarin.Forms) 如何从上下文菜单中获取所选项目 - How to get the selected item from a context menu 如何使用contextmenu复制将listview子项文本复制到剪贴板? - How to copy listview sub item text to clipboard using contextmenu copy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM