Question

I could remove a selected item from my ListView of an image by this way:

private void Delte_Photo_Click(object sender, RoutedEventArgs e)
        {
            if (ListViewImage.SelectedItem == null)
            {
                MessageBox.Show("Please select an image to be removed...");
                return;
            }

            ImageFileCollectionViewModel viewModel = ListViewImage.DataContext as ImageFileCollectionViewModel;
            if (viewModel != null)
            {
                ImageFileViewModel image = ListViewImage.SelectedItem as ImageFileViewModel;//ListViewImage is the listBox
                if (image != null)
                {
                    //remove physical file from disk:
                    File.Delete(image.FileName);
                    //remove item from ObservableCollection:
                    viewModel.AllImages.Remove(image);
                }
            }
        }

How can i remove multiple images? How can i delete all the images? by button click.

Was it helpful?

Solution

Iterate over your imagecollection via foreach

foreach(var image in viewModel.AllImages.ToList()){
    File.Delete(image.FileName);
    viewModel.AllImages.Remove(image);
}

to remove multiple instances at once:

        // let's say these are the positions of your items to remove
        var positions = new List<int>() { 1, 4, 6 };
        var list = viewModel.AllImages.ToList();

        // this removes all objects on the given index
        foreach (var position in positions)
        {
           list.RemoveAt(position);
        }

If you want to remove by objectreference iterate through a collection of objects to delete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top