0

I am using xaml and wpf. I have todo list that is in a listbox populated by a collection. I currently have the capability to remove one item in the list at a time. How would I edit this to be able to select and delete multiple at a time?

APotatoe
  • 1
  • 1

1 Answers1

0

U could use SelectedItems ListBox property and pass it as command parameter. Then in ViewModel you will be able to get every selected item.

Remember to set

SelectionMode="Extended" //or Multiple

on your ListBox.

Example:

This is my button with binding to command:

<Button Content="⇅"
    ToolTip="Use ctr+click or shift+click to select more than one item"
    Command="{Binding SwapVerticallyCommand}"
    CommandParameter="{Binding ElementName=RightListBox, Path=SelectedItems}" />

As you can see I pass SelectedItems as command arguments. Then in VM this method is executed (similar to your RemoveTodoItem):

private void SwapVertically(IList obj)
    {
        if ( obj[0] is Slot first && obj[1] is Slot second )
        {
            (first.Target, second.Target) = (second.Target, first.Target); //swap places
        }
    }

IList is passed as parameter and you can get values from it. So, if your command gets obj then cast it to IList, but better solution would be to define type of object passed to command from View - use IList instead of object.

Skomorow
  • 3
  • 5