问题
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?
回答1:
The binding isn't working because the ContextMenu
exists outside of your control's visual tree, and so it's impossible for it to find the ListBox
. In fact I'm surprised it's invoking your command without the usual incantation to get the data context of the associated control:
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" >
Regardless, you can use the answer suggested here, or I can suggest an alternate implementation: add a SelectedPerson
property to your view-model:
private Person selectedPerson;
public Person SelectedPerson
{
get { return selectedPerson; }
set
{
selectedPerson = value;
RaisePropertyChanged(); // or whatever your implementation is
}
}
Your XAML would be simple, too:
<ListView ItemsSource="{Binding Persons}"
SelectedItem="{Binding SelectedPerson}">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="EMail"
Command="{Binding WriteMailCommand}"
CommandParameter="{Binding SelectedPerson}" />
</ContextMenu>
</ListBox.ContextMenu>
</ListView>
来源:https://stackoverflow.com/questions/33548871/listbox-contextmenu-get-selected-item-via-command