简体   繁体   English

从一个VM上的列表中删除项目,然后刷新另一个VM上的列表

[英]Removing an item from a list on one VM and refreshing the list on another VM

In my project, using MVVM and C#. 在我的项目中,使用MVVM和C#。 I have 2 ViewModels. 我有2个ViewModel。 This first loads a List and creates an Observable Collection List from it. 首先,它会加载一个列表并从中创建一个可观察的集合列表。

private ObservableCollection<Problems> _ProblemsList;
    public ObservableCollection<Problems> ProblemsList
    {
        get 
        {
            return _ProblemsList;
        }
        set
        {
            _ProblemsList = value;
            OnPropertyChanged(null);
        }
    }
    public List<Problems> Problems { get; set; }

Once the List is filled, the Observable is populated from it. 列表填充后,从中填充Observable。 I then load the observable into a gridview. 然后,将可观察对象加载到gridview中。

Once you select a problem on the gridview, you are taken to a new View. 在gridview上选择问题后,将带您进入新视图。 This View allows you to alter the data of the problem. 此视图使您可以更改问题的数据。 One key field is changed, lets call it Validated (bool). 一键字段已更改,我们称之为“已验证”(布尔)。 Validated is changed from false to true, and once you click done, you should return to the first View, with the lists. 将Validated从false更改为true,单击完成后,应返回到带有列表的第一个视图。 My problem is, now that the problem has been altered, it should be removed from the list. 我的问题是,既然问题已被更改,则应将其从列表中删除。

So in my second View's VM code, I have: 因此,在第二个View的VM代码中,我有:

protected void DeleteFromList()
    {
        long id = selectedProblem.ID;
        ProblemsList.ProblemsList.Remove(x => x.ID == id);
    }

The first ProblemsList is the instance of the previous VM, ProblemsListingViewModel.cs I sent the instance through to the next VM, and they are filled and declared: 第一个ProblemsList是以前的VM的实例,我将实例发送到下一个VM的ProblemsListingViewModel.cs,并填充并声明了它们:

public InitiateWorkViewModel(CallProblems selectedProblem, ProblemsListingViewModel ProblemsList)

selectedProblem is just the problem that was selected from the list on the first View selectedProblem只是从第一个视图的列表中选择的问题

My issue is that the observable doesn't want to refresh once I navigate back. 我的问题是,一旦导航返回,可观察对象就不希望刷新。 I change the data, the Validated is changed, the problem is removed from the list in my second code input above, but it doesn't reflect when I navigate back. 我更改了数据,更改了Validated,从上面第二个代码输入中的列表中删除了该问题,但是当我向后导航时,该问题并没有反映出来。 How do I correct this? 我该如何纠正?

Basically, I want to remove the problem from the list, so that the user will know that the work has been done on that problem and they can't choose it again. 基本上,我想从列表中删除问题,以便用户知道该问题的工作已经完成,并且他们无法再次选择它。 I don't want to delete the whole record (from the database), just from the selectable list 我不想删除整个记录(从数据库中),而只是从可选列表中删除

If there are any further problems, please comment and I will edit the question accordingly 如果还有其他问题,请发表评论,我会相应地编辑问题

I believe that your problem is because you are calling this...: 我相信您的问题是因为您正在打电话给这个...:

ProblemsList.ProblemsList.Remove(x => x.ID == id);

... and expecting the UI to update itself. ...并期望用户界面能够自我更新。

Unfortunately, the ObservableCollection.Remove method does not raise the INotifyPropertyChanged.PropertyChanged event and so the UI is not updated. 不幸的是, ObservableCollection.Remove方法不会引发INotifyPropertyChanged.PropertyChanged事件,因此不会更新UI。 There are a few ways to fix this situation, as all you have to do is to raise the INotifyPropertyChanged.PropertyChanged event when you add or remove the items from the collection. 有几种方法可以解决此问题,因为您要做的就是在添加或删除集合中的项目时引发INotifyPropertyChanged.PropertyChanged事件。

The simplest is to attach a handler to the ProblemsList.CollectionChanged event in your view model. 最简单的方法是将处理程序附加到视图模型中的ProblemsList.CollectionChanged事件。 You can then raise the INotifyPropertyChanged.PropertyChanged event each time an item is added or removed from the collection: 然后,每次在集合中添加或删除项目时,就可以引发INotifyPropertyChanged.PropertyChanged事件:

private void ProblemsList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    NotifyPropertyChanged("ProblemsList");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM