Jak przekazać ListBox.SelectedIndex
do MenuItem.CommandParameter
wewnątrz ContexMenu
bez dodawania SelectedIndex
do view modelu?
public ObservableCollection<string> SelectedWords { get; set; };
public ICommand DeleteWordCommand { get; }
public ViewModel()
{
DeleteWordCommand = new RelayCommand<int>(HandleWordDeletion);
}
private void HandleWordDeletion(int selectedIndex)
<ListBox ItemsSource="{Binding SelectedWords}">
<ListBox.InputBindings>
<KeyBinding Key="Delete" Command="{Binding DeleteWordCommand}" CommandParameter="{Binding SelectedIndex, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}}" />
</ListBox.InputBindings>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=ListBox}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu Tag="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Delete"
Command="{Binding PlacementTarget.Tag.DeleteWordCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter=""
/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>