使 ViewModel 属性对 Silverlight 中的绑定可见

发布于 2024-07-30 02:19:01 字数 842 浏览 4 评论 0原文

我将一个对象(名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2024-08-06 02:19:01

看起来当执行达到 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文