c# - 如何通过上下文菜单中的复制命令复制选定的 ListView 项

标签 c# wpf xaml listview copy

我有一个 ListView ,我想通过在上下文菜单中选择复制命令来复制所选项目。我可以使用复制命令创建上下文菜单,如下图所示:

enter image description here

问题是当我选择一个行项目并右键单击它时,上下文菜单不会显示,我无法选择复制命令来复制所选项目。当我右键单击 ListView 中的空白区域时,将显示上下文菜单。

enter image description here

代码如下:

.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:

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

最佳答案

像这样的东西会让你使用 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 >

因此,在 ItemTemplate 中定义它,以便在选择每个项目时能够使用它。

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

没什么特别的,只是为了举例。

还有一件事,如果您仍然希望在单击空白区域时它仍然可用,请在 ListView 上也添加一个 ContextMenu:

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

关于c# - 如何通过上下文菜单中的复制命令复制选定的 ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32111154/

相关文章:

c# - 代码中的网址不会破坏构建

c# - 当 ItemsSource 与 ResourceDictionaries 或 MergedDictionaries 一起使用 setter 属性时,MenuItem 样式

c# - Asp.net (MVC) 和 WPF (MVVM) 的比较 - 是否有等效的数据绑定(bind)?

c# - 删除 DataGrid 中自动生成的行

c# - TrySZBinarySearch 在哪里实现的?

c# - 在 C# 代码中创建元素时出现 WPF 绑定(bind)错误

c# - 具有网格布局的自定义 ItemsControl

c# - 如何使用 Windows 10 创建特定于设备系列的项目,例如在 W8.1/WP8.1 上?

c# - RaisePropertyChanged 无法更新 UI

c# - Double ParseDouble - 输入字符串的格式不正确 - 如何修复?