Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ImageFileCollectionViewModel的ListView中删除所选项目_C#_Wpf_Listview - Fatal编程技术网

C# 从ImageFileCollectionViewModel的ListView中删除所选项目

C# 从ImageFileCollectionViewModel的ListView中删除所选项目,c#,wpf,listview,C#,Wpf,Listview,我试图从列表视图和目录中删除所选项目文件,但未能成功。我怎样才能去掉这个 string destination_dir = System.IO.Directory.GetCurrentDirectory() + @"./4x6"; public ImggLList() { InitializeComponent(); ListViewImage.Items.Clear(); DataContextChanged += OnDataContextChange

我试图从列表视图和目录中删除所选项目文件,但未能成功。我怎样才能去掉这个

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);
            }

        }
private-List-copyOfSelection;
私有ImageFileCollectionViewModelImageFileCollection;
私有无效数据块照片点击(对象发送者,路由目标)
{
copyOfSelection=imageFileCollection.SelectedImageFileViewModels.Cast().ToList();
foreach(ImageFileViewModel ifvm在copyOfSelection中)
{
复制选择。删除(ifvm);
文件删除(目的地目录);
}
}
NullExeception错误:
for(int i=0;i
我不相信您可以修改foreach循环中的列表:这里@akousmata:我如何解决这个问题?请指导我。使用我提供的两个链接建议的标准for循环。我已经初始化了ImageFileCollectionViewModel和ImageFileViewModel,在您的
OnDataContextChanged
方法中将
SelectedImageFileViewModels
设置为null时,我发现null错误。我不确定您的代码是如何工作的,但当您尝试引用该对象时,该对象很可能是空的。但这与如何从for循环的列表中删除项无关。解决此问题的最佳方法是什么?非常感谢。
for (int i = 0; i < copyOfSelection.Count; i++)
{
    copyOfSelection.RemoveAt(i);
    File.Delete(destination_dir);        
}