Question

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();
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top