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!
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).