从ImageFileCollectionViewModel中删除ListView中的所选项

时间:2014-01-21 14:57:34

标签: c# wpf listview

我正在尝试从列表视图中删除所选的Item文件,也从目录中删除,但我无法成功。我怎样才能删除它。?

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;

        }

按钮功能:

 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错误: enter image description here

1 个答案:

答案 0 :(得分:1)

for (int i = 0; i < copyOfSelection.Count; i++)
{
    copyOfSelection.RemoveAt(i);
    File.Delete(destination_dir);        
}