简体   繁体   English

使用MVVM Light绑定到ObservableCollection

[英]Binding to ObservableCollection with MVVM Light

I have a MainViewModel that contains a reference to an ObservableCollection : 我有一个MainViewModel ,其中包含对ObservableCollection的引用:

public ObservableCollection<SomeClass> ListOfPeople
{
    get
    {
        return MyClass.BaseCollection;
    }
}

BaseCollection is also an instance of ObservableCollection<SomeClass> . BaseCollection还是ObservableCollection<SomeClass>的实例。 ListOfPeople is bound to a ListBox control on the second page - the application starts with the first page, initiates the download process to populate BaseCollection and switches to the second page while the download is still in progress. ListOfPeople绑定到第二页上的ListBox控件-应用程序从第一页开始,启动下载过程以填充BaseCollection ,并在下载仍在进行时切换到第二页。

The problem is that when the binding occurs, BaseCollection is null and therefore the ListBox is not populated. 问题在于,发生绑定时, BaseCollection为null,因此不会填充ListBox However, even when the download process finishes, the ListBox still remains empty. 但是,即使下载过程完成,ListBox仍然保持为空。 I am assuming this is happening because BaseCollection isn't notifying the proper instance about existing changes to the collection, but I am not sure. 我以为这是因为BaseCollection没有将有关集合的现有更改的适当实例通知给我,但我不确定。

BaseCollection has items inside it - I confirmed it. BaseCollection里面有项目-我确认了。

Any suggestions on how I can work around the issue? 关于如何解决此问题的任何建议? Anyone here binding to an ObservableCollection via MVVM Light just like I showed above? 有人像我上面显示的那样通过MVVM Light绑定到ObservableCollection吗?

If you donot want to instantiate an empty ListOfPeople in the constructor and use this instance for database-loading you have to do this: 如果您不想在构造函数中实例化一个空的ListOfPeople并将此实例用于数据库加载,则必须执行以下操作:

After loading of ListOfPeople is completed, your MainViewModel must call RaisePropertyChanged("ListOfPeople"); 完成ListOfPeople加载后,您的MainViewModel必须调用RaisePropertyChanged("ListOfPeople"); to tell the view that the data has changed. 告诉视图数据已更改。

Background: Thanks to ObservableCollection MyClass.BaseCollection.Add() updates the gui. 背景:感谢ObservableCollection MyClass.BaseCollection.Add()更新了gui。 As soon as MyClass.BaseCollection = new Obser... is called there is no more update of the gui since the gui holds a reference to the old contend of MyClass.BaseCollection . 一旦MyClass.BaseCollection = new Obser...被调用,就不再进行gui的更新,因为gui拥有对MyClass.BaseCollection的旧内容的引用。 mvvm-light-RaisePropertyChanged() tells the gui to update its reference to a new collection mvvm-light-RaisePropertyChanged()告诉gui更新对新集合的引用

I haven't worked on MVVM Light, so sorry if there is something specific about it that i am missing. 我尚未在MVVM Light上工作,所以对不起我是否缺少某些特定的东西。

Looking at your implementation, 看你的实现,

public ObservableCollection<SomeClass> ListOfPeople
{
    get
    {
        return MyClass.BaseCollection;
    }
}

This code should work, and the control which is binded to this source should get propert updated without being concerned about the actual source where the instance of observable is created. 该代码应该起作用,并且绑定到该源的控件应该获得适当的属性更新,而不必考虑创建observable实例的实际源。

Thus, the only possible problem here could be that your MyBase.BaseCollection is null in the begining. 因此,这里唯一可能的问题可能是MyBase.BaseCollection在开始时为null。 So, if you avoid that situation and create an empty collection where you have declared this observable item, and then trigger your downloading process the way it is, then everything should work fine. 因此,如果避免这种情况,并在声明此可观察项的地方创建一个空集合,然后按原样触发下载过程,则一切应该正常进行。

Hope this would be of help. 希望这会有所帮助。

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

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