WP7难以将数据绑定到列表框项目源-无法刷新 [英] WP7 difficulties binding data to listbox itemssource - won't refresh

查看:48
本文介绍了WP7难以将数据绑定到列表框项目源-无法刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在WP7应用程序上有一个详细信息页面,其中显示了一个列表框,其中显示了来自应用程序的一些数据.

So I have a detail page on my WP7 app that shows a listbox showing some data from my app.

在后面的代码中,我将列表框itemssource =设置为对象的集合.到目前为止非常简单.在同一屏幕上,我还有几个文本框和一个按钮,可让我添加到该对象集合中.

In the codebehind, I set the listbox itemssource = to a collection of objects. Pretty simple so far. I also have few text boxes and a button on the same screen that lets me add to that object collection.

理想情况下,当我导航到页面时,它应该在列表框中显示集合的内容.如果单击添加,它将添加另一个对象到集合中,然后刷新列表框以显示新更新的内容.

Ideally when I navigate to the page, it should show the contents of the collection in the listbox. If I click add, it should add another object to the collection and then refresh the listbox to show the newly updated contents.

我在onNavigatedTo事件中设置了itemsSource,并且在按钮单击事件中也设置了itemsSource.我的按钮单击事件只是从文本框中提取文本,使用文本创建我的对象的实例,将对象添加到集合中,然后重新设置itemsSource

I set the itemsSource in the onNavigatedTo event, and I set the itemsSource in my button click event as well. My button click event just pulls the text from the textbox, creates an instance of my object with the text, adds the object to the collection, and re-sets the itemsSource

问题是,仅当我按下后退"按钮并重新进入页面时,列表框才会更新.在我的按钮单击事件上设置itemSource似乎没有任何作用-列表框不会刷新.

The problem is, the listbox only updates when i hit the back button and re-enter the page. Setting the itemSource on my button click event doesn't seem to do anything - the listbox won't refresh.

有人知道我如何刷新列表框吗?

Does anyone know how I can refresh the listbox?

推荐答案

如果使用ObservableCollection,则列表框将自动反映添加项和删除项.

If you use ObservableCollection the listbox will automatically reflect additons and deletions.

如果您尝试在没有ObservableCollection的情况下执行此操作,那么我已经看到,如果在重新分配新列表之前将ItemsSource设置为null,这将起作用.

If you try to do this without ObservableCollection I've seen this will work if you set the ItemsSource to null, before reassigning the new list.

这篇关于WP7难以将数据绑定到列表框项目源-无法刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆