如何在 Silverlight 中以编程方式用数据条目填充列表框?
尝试以编程方式填充列表框,我编写了以下代码:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
TestDBContext context = new TestDBContext();
context.Load(context.GetTaskQuery());
var taskList = GetTasks();
foreach (var t in taskList)
{
ListBoxTaskItems.Items.Add(t);
}
}
private List<TaskItem> GetTasks()
{
var tasks = from t in context.Tasks
select new TaskItem(t);
return tasks.ToList();
}
问题是上面的代码总是返回一个空的列表框。有谁知道如何修改现有代码或其他方式以编程方式用数据条目填充列表框?
编辑#1:在调试时,我注意到 GetTasks() 方法在 context.GetTaskQuery() 之前执行,我猜这就是 ListBox 为空的原因。不过,我不知道如何修复代码以填充列表框。
谢谢你!
Trying to fill the listbox programatically I have written the following code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
TestDBContext context = new TestDBContext();
context.Load(context.GetTaskQuery());
var taskList = GetTasks();
foreach (var t in taskList)
{
ListBoxTaskItems.Items.Add(t);
}
}
private List<TaskItem> GetTasks()
{
var tasks = from t in context.Tasks
select new TaskItem(t);
return tasks.ToList();
}
The problem is that the code above alway returns an empty ListBox. Does anyone know how to modify the existing code or another way to programatically fill the listbox with the data entries?
Edit #1: While debugging I've noticed that GetTasks() method is executed before context.GetTaskQuery() and I gues this is the reason for an empty ListBox. Nevertheless I don't know how to fix the code in order to populate the ListBox.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有人发布了另一个解决方案,然后出于某种原因将其删除,但这可能会更好。我刚刚在我正在处理的一些代码中尝试过它,发现它更快:(
用它代替 foreach 语句)。
Somebody posted another solution, then for whatever reason deleted it, but that might work better. I just tried it in some code I'm working on and found it was faster:
(Substitute this for the foreach statement).
我会在 foreach 语句中尝试这样的操作:
我做了类似的事情,并且在将网格添加到 ListBox 时它可以工作,因此只要 TaskItem 是可以接受的内容,就应该这样做。
I'd try it like this inside your foreach statement:
I do something similar and it works when adding Grids to a ListBox, so as long as a TaskItem is something acceptable as Content that should do it.