Reputation: 1939
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.
Upvotes: 0
Views: 774
Reputation: 2487
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.
Upvotes: 1