使 ViewModel 属性对 Silverlight 中的绑定可见
我将一个对象(名为 Client)和 ObservableCollection(名为 Contacts)属性绑定到我的 Silverlight 视图。 我的 Client 类在 is 和名为 Contacts 的 ObservalbeCollection 上有一些字符串属性。 我的视图模型上有一个名为 Client 的属性(它实现 INotifyPropertyChanged),其中包含 Client 对象。 如果我将视图中的 ListBox 绑定到对象上的 ObervableCollection,如下所示:
ItemsSource="{Binding Path=Client.Contacts, Mode=TwoWay}"
并将联系人项添加到集合中,视图将正确更新并显示我新添加的联系人。 这一切都很好。
如果我像这样在 ViewModel 上创建一个 Contacts 属性 公共 ObservableCollection 联系人 { 得到 { 返回客户.联系人; } 并将 ListBox 绑定到
ItemsSource="{Binding Path=Contacts, Mode=TwoWay}"
视图永远不会更新。
我将联系人项添加到客户端,如下所示:
Client.Contacts.Add(newContact)
为什么联系人列表框不更新? 我怎样才能改变它呢? Client.Contacts 绑定可以使用吗? 添加新联系人后在代码中打断会显示新的新联系人对象已添加到集合中,但视图看不到添加内容。
I am binding an object(named Client) with and ObservableCollection(named Contacts) property to my Silverlight view. My Client class has a few string properties on is and the ObservalbeCollection called Contacts. There is a property on my viewmodel named Client(Which implements INotifyPropertyChanged) that contains the Client object. If I bind the ListBox in my view to the ObervableCollection on the object like this:
ItemsSource="{Binding Path=Client.Contacts, Mode=TwoWay}"
and add an Contact item to the collection, the view updates properly and I am shown my newly added Contact. This all works great.
If I create a Contacts property on my ViewModel like this
public ObservableCollection Contacts
{
get
{
return Client.Contacts;
}
and bind the ListBox to
ItemsSource="{Binding Path=Contacts, Mode=TwoWay}"
the view is never updated.
I add the Contact item to the Client like this:
Client.Contacts.Add(newContact)
Why doesn't the ListBox of Contacts update? How can I change this so it does? Is the Client.Contacts binding OK to use? Putting a break in the code after adding the new Contact shows that new new Contact object is getting added to the collection but the view is not seeing the addition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来当执行达到 ItemsSource="{Binding Path=Contacts, Mode=TwoWay}" 时绑定尚未执行。 您能否验证是否在 ViewModel 的构造函数中实例化集合,因为我觉得在绑定时 Contacts 实例未设置(null)
It looks like when the binding havent executed when execution reached ItemsSource="{Binding Path=Contacts, Mode=TwoWay}". Can you verify whether you instantiating the collection in the constructor of the ViewModel, because I feel at the binding time the Contacts instance not set(null)