0

I'm trying to remove the selected Item file from the list-view and also from the directory but I couldn't succeed. How can i remove this.?

string destination_dir = System.IO.Directory.GetCurrentDirectory() + @"./4x6";
    public ImggLList()
    {
    InitializeComponent();
    ListViewImage.Items.Clear();
    DataContextChanged += OnDataContextChanged;


    ImageFileCollectionViewModel ImagesViewModel = new ImageFileCollectionViewModel();
    ImageFileControler.CompleteViewList(ImagesViewModel, destination_dir);
    ListViewImage.DataContext = ImagesViewModel;
    }

OnDataContextChanged

private ImageFileCollectionViewModel _currentDataContext = null;
        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (_currentDataContext == DataContext) return;

            if (_currentDataContext != null)
                _currentDataContext.SelectedImageFileViewModels = null;

            _currentDataContext = DataContext as ImageFileCollectionViewModel;
            if (_currentDataContext != null)
                _currentDataContext.SelectedImageFileViewModels = ListViewImage.SelectedItems;

        }

Button Function:

 private List<ImageFileViewModel> copyOfSelection;

        private ImageFileCollectionViewModel imageFileCollection;
        private void Delte_Photo_Click(object sender, RoutedEventArgs e)
        {
            copyOfSelection = imageFileCollection.SelectedImageFileViewModels.Cast<ImageFileViewModel>().ToList();

            foreach (ImageFileViewModel ifvm in copyOfSelection)
            {
                copyOfSelection.Remove(ifvm);
                File.Delete(destination_dir);
            }

        }

NullExeception Error: enter image description here

linguini
  • 1,939
  • 5
  • 49
  • 79
  • I don't believe you can modify a list in a foreach loop: http://stackoverflow.com/questions/6294983/modifying-list-inside-foreach-loop and here http://stackoverflow.com/questions/308466/how-to-modify-or-delete-items-from-an-enumerable-collection-while-iterating-thro – akousmata Jan 21 '14 at 15:01
  • @akousmata: How can i fix this? Please guide me. – linguini Jan 21 '14 at 15:03
  • Use a standard for loop as both of the links I provided suggest. – akousmata Jan 21 '14 at 15:07

1 Answers1

1
for (int i = 0; i < copyOfSelection.Count; i++)
{
    copyOfSelection.RemoveAt(i);
    File.Delete(destination_dir);        
}
akousmata
  • 1,005
  • 14
  • 34
  • I have initialized the ImageFileCollectionViewModel & ImageFileViewModel, i'm getting null error – linguini Jan 21 '14 at 15:13
  • In your `OnDataContextChanged` method you are setting the `SelectedImageFileViewModels` to null. I'm not sure how your code works, but chances are that object is null when you are trying to reference it. That has nothing to do with how to remove an item from a list in for loop though. – akousmata Jan 21 '14 at 15:30
  • What is the best way to fix this problem? Thank you – linguini Jan 21 '14 at 15:33