不断更新ListView的ItemsSource?

发布于 2024-08-21 06:51:34 字数 1034 浏览 1 评论 0原文

我有一个 ListView,我将其 ItemsSource 设置为列出所有分配(我的 SQL 数据库中的一个表,ORM 是 LINQ to SQL),如下所示:(

ltvAssignments.ItemsSource = _repo.ListAssignments();

这段代码恰好在调用 InitializeCompenent() 之后)它,我添加了一个示例:(

Assignment sample1 = new Assignment()
        {
            Title = "A Test",
            Start = DateTime.Now,
            Due = DateTime.Now,
            Kind = (byte) Kind.Assignment,
            Priority = (byte) Priority.Medium,
        };
        _repo.CreateAssignment(sample1);
        _repo.SaveChanges(); 

其中 _repo 是我的存储库,因为我正在使用存储库模式)当我在设置 ListView 的 ItemsSource 之前放置这段代码时,示例会显示。但是,当这段代码位于设置 ItemsSource 之后的任何位置时,示例不会显示。如何在每次添加作业时不断更新 ItemsSource?
我的存储库:

public interface IAssignmentRepository
{
    Assignment CreateAssignment(Assignment assignmentToCreate);
    void DeleteAssignment(Assignment assignmentToDelete);
    Assignment GetAssignment(int id);
    IEnumerable<Assignment> ListAssignments();
    void SaveChanges();
}

I have a ListView that I set it's ItemsSource to list all the Assignments (a table in my SQL Database, ORM is LINQ to SQL) like so:

ltvAssignments.ItemsSource = _repo.ListAssignments();

(This bit of code is exactly after InitializeCompenent() is called) And for the heck of it, I added a sample:

Assignment sample1 = new Assignment()
        {
            Title = "A Test",
            Start = DateTime.Now,
            Due = DateTime.Now,
            Kind = (byte) Kind.Assignment,
            Priority = (byte) Priority.Medium,
        };
        _repo.CreateAssignment(sample1);
        _repo.SaveChanges(); 

(where _repo is my Repository because I am using the repository pattern) When I put this bit of code before I set the ListView's ItemsSource, the sample shows. BUT when this bit of code is anywhere after ItemsSource is set, the sample doesn't show. How can I constantly update the ItemsSource everytime an Assignment is added?
My IRepository:

public interface IAssignmentRepository
{
    Assignment CreateAssignment(Assignment assignmentToCreate);
    void DeleteAssignment(Assignment assignmentToDelete);
    Assignment GetAssignment(int id);
    IEnumerable<Assignment> ListAssignments();
    void SaveChanges();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

莫相离 2024-08-28 06:51:34

我认为原因是您的 IAssignmentRepository 没有实现 INotifyCollectionChanged 接口。

当您在设置 ItemsSource 之前添加数据时,一旦 GUI 更新,数据就已经存在,可供查看。但是,当您进行后续更改时,由于存储库不会通知数据绑定控件,因此不会发生更新。

我还假设您已正确设置 DataContext。

I think the reason is that your IAssignmentRepository doesn't implement the INotifyCollectionChanged interface.

When you add the data before setting the ItemsSource, the data is already there for viewing as soon as the GUI updates. But when you make subsequent changes, since the repository won't notify the databound control, no updates occur.

I'm also assuming that you've set the DataContext properly.

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