16

I am figuring out a way to Select Multiple items in list view and deleting them on a certain action. What I can't figure out is, how should I have these multiple items selected? I would think there is a list that I would need to add them all into, but what's the best way to approach this situation, do you have any ideas? Thanks! -Kevin

5 Answers 5

27

Set SelectionMode to Multiple or Extended and iterate through theSelectedItems in your ListView.

Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting that SelectionMode.Multiple does not include the ability to use Shift+Click to select consecutive items or Ctrl+A to select all. Seems like a bug/oversight.
13

I would suggest do not use the SelectedItems property of ListView, instead bind the Selected property of the single ListViewItem, to a corresponding ViewModel class. After this, the only thing you need to do is to find all ViewModel object that have bound the Selected property TRUE, remove them from model collection (if you do remove) and refresh UI. If the collection is ObservableCollection, the UI will be refreshed automatically. Good Luck.

3 Comments

Could you please provide any reasons why you should not use the SelectedItems property?
because it makes design more complicated and that may lead to bugs. If you have a Selected Property on each object that is in the ItemsSource you can bind each item very easily like this: <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}"> <Setter Property="IsSelected" Value="{Binding Selected, Mode=TwoWay}" /> </Style>
@NomanKhan Should be ListViewItem not ListBoxItem; also no need for BasedOn I think... But good comment, helped me!
11

Arcturus answer is good if you're not using MVVM. But if you do and your ItemsSource is binded to some ObservableCollection of objects in your ViewModel, I would recommend Tigran answer, with Noman Khan clarification.

This is how it would look like:

<ListView ItemsSource="{Binding SomeListViewList}">
    <ListView.Resources>
       <Style TargetType="{x:Type ListViewItem}">
          <Setter Property="IsSelected" Value="{Binding SomeItemSelected, Mode=TwoWay}" />
       </Style>
    </ListView.Resources>
    ...
</ListView>

In View Model you would have object: public ObservableCollection<SomeItem> SomeListViewList{ get; set; }

SomeItem Class would include a Selected property:

public class SomeItem
{
    public string SomeItemName { get; set; }

    public string SomeItemNum { get; set; }

    public bool SomeItemSelected { get; set; }
}

Then you could iterate/run over the list and get the ones that are selected:

foreach (var item in SomeListViewList)
   if (item.SomeItemSelected)
      // do something

Comments

6

You can do one of the following...

ListView.SelectionMode = SelectionMode.Extended in code-behind or

<ListView SelectionMode="Extended"></ListView> in XAML

you also have 'multiple' selectionMode yet you could rather go for 'extended' which allows the user to select multiple items only using shift modifier.

For deleting the items selected you could use the ListView.SelectedItems Propery as follows

while( myListView.SelectedItems.Count > 0 )
{
    myListView.Items.Remove(list.SelectedItems[0]);
}

[or you could use the SelectedIndices property]

Hope this will avoid the issue you encountered :)

Cheers!

Comments

0

Get success also WPF listview by writing

while (lvJournalDetails.SelectedItems.Count > 0)
{
    lvJournalDetails.Items.Remove(lvJournalDetails.SelectedItem);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.