如何在 Silverlight 中以编程方式用数据条目填充列表框?

发布于 2024-10-25 07:54:20 字数 751 浏览 0 评论 0原文

尝试以编程方式填充列表框,我编写了以下代码:

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 技术交流群。

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

发布评论

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

评论(2

Oo萌小芽oO 2024-11-01 07:54:20

有人发布了另一个解决方案,然后出于某种原因将其删除,但这可能会更好。我刚刚在我正在处理的一些代码中尝试过它,发现它更快:(

Binding bind = new Binding();

bind.Source = GetTasks();

ListBoxTaskItems.SetBinding(ListBox.ItemsSourceProperty, bind);

用它代替 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:

Binding bind = new Binding();

bind.Source = GetTasks();

ListBoxTaskItems.SetBinding(ListBox.ItemsSourceProperty, bind);

(Substitute this for the foreach statement).

沉鱼一梦 2024-11-01 07:54:20

我会在 foreach 语句中尝试这样的操作:

ListBoxItem listBoxItem = new ListBoxItem();

listBoxItem.Content = t;

ListBoxTaskItems.Items.Add(listBoxItem);

我做了类似的事情,并且在将网格添加到 ListBox 时它可以工作,因此只要 TaskItem 是可以接受的内容,就应该这样做。

I'd try it like this inside your foreach statement:

ListBoxItem listBoxItem = new ListBoxItem();

listBoxItem.Content = t;

ListBoxTaskItems.Items.Add(listBoxItem);

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.

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