[英]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.