如果您正在寻找ListViewItem的DataContext,您可以这样做:
CommandParameter = “{结合}”
编辑 – 这是我尝试过的:
public partial class MainWindow : Window
{
private ObservableCollection list = new ObservableCollection();
public MainWindow()
{
InitializeComponent();
list.Add(new Person() { Name = "Test 1"});
list.Add(new Person() { Name = "Test 2"});
list.Add(new Person() { Name = "Test £"});
list.Add(new Person() { Name = "Test 4"});
this.DataContext = this;
}
public static ICommand MyCommand //this is a command, I want to be able execute from context menu of each item
{
get
{
return new DelegateCommand(
a => Console.WriteLine(a.Name),
a => true);
}
}
public ObservableCollection Items
{
get
{
return this.list;
}
}
}
public class Person
{
public string Name { get; set; }
}
和xaml:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ListView1="clr-namespace:ListView1" Title="MainWindow" Height="350" Width="525">