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?
我的 IRepository:

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

1 回答 1

3

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.

于 2010-02-12T20:01:15.067 回答