I have this View:
<StackPanel>
<StackPanel.DataContext>
<local:MainViewModel />
</StackPanel.DataContext>
<ListView ItemsSource="{Binding Persons}" x:Name="xamlPersonList">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="EMail" Command="{Binding WriteMailCommand}" CommandParameter="{Binding ElementName=xamlPersonList,Path=SelectedItem}" />
</ContextMenu>
</ListBox.ContextMenu>
</ListView>
</StackPanel>
I want to get the selected item (or the clicked item) and do some stuff with it inside my Command-Method. This is my Ctor and Command-Method of my ViewModel:
public ICommand WriteMailCommand { get; private set; }
public MainViewModel()
{
_persons = new ObservableCollection<Person>();
for (int i = 0; i < 10; i++)
{
_persons.Add(new Person()
{
ID = i,
Name = "Robert " + i
});
}
WriteMailCommand = new RelayCommand<object>(WriteMailMethod);
}
private void WriteMailMethod(object obj)
{
}
The obj parameter is always null. I don't know what I am missing here?! I tried this solution: How to pass listbox selecteditem as command parameter in a button?