views:

383

answers:

1

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();
}
+2  A: 

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.

Dave
Actually I did not set the DataContext property.
Mohit Deshpande
Is that a problem?
Mohit Deshpande
Absolutely, you need to set the DataContext or the databinding won't work out. Here's a quick test (after your repo implements INotifyCollectionChanged) -- run your app, and see if you get databinding errors in the output window when you update your repository.To eliminate any other possibilities, try implementing a test container that inherits from ObservableCollection<T>, since that has never failed me. It implements INotifyCollectionChanged for you. If you use this and you don't get updates, you will know you need to get the DataContext set properly.
Dave
So in the event, CollectionChanged, I just say ltvAssignments.ItemsSource = _repo.ListAssignments();? And can I set the DataContext to _repo.ListAssignments();?
Mohit Deshpande
it's best if I link you here, which gives all of the gory details. http://www.c-sharpcorner.com/UploadFile/yougerthen/612182008101211AM/6.aspx As far as the DataContext goes, you could set the ListView DataContext to ListAssignments, then your XAML should have ItemsSource={Binding}. Of course, I think you're setting ItemsSource in code behind, so I believe you would just leave your ItemsSource as-is.
Dave
**Dave is incorrect about the need to set DataContext**. `DataContext` need not be set (or in fact used in any way) for `ListView.ItemsSource` to work properly and fully support updates through `INotifyCollectionChanged`. I usually do recommend people use `DataContext` and bind `ItemsSource`, but the way you are doing it you don't need `DataContext` at all. In any case, you should either use `DataContext` **instead of** setting `ItemsSource` directly or not use it at all. Assigning to both ItemsSource and DataContext is redundant, inefficient and confusing to anyone looking at your code.
Ray Burns
**Dave is correct**, however, that your problem is that you did not implement `INotifyCollectionChanged`.
Ray Burns
@Ray: I didn't know that. I always set the DataContext, but then again, I'm always binding to a ViewModel, and not an individual control.
Dave
Thanks everone, but in my event, do I need to reset the ItemsSource with: ltvAssignments.ItemsSource = _repo.ListAssignments(); everytime?
Mohit Deshpande
If ListAssignments is a method that returns an IEnumerable, then yes. If ListAssignments is a property that returns a reference to the ObservableCollection that the object is maintaining, then no.
Robert Rossney