views:

671

answers:

2

I have a list view that has a column databound to a list.Count see below:

<ListView.View>
    <GridView>
        <GridViewColumn Header="Contacts" DisplayMemberBinding="{Binding Path=Contacts.Count}"/>
        <GridViewColumn Header="Notes" DisplayMemberBinding="{Binding Path=Notes.Count}"/>
    </GridView>
</ListView.View>

The List implements INotifyCollectionChanged. But when I add an item to the list the listview column does not get refreshed. am I doing something wrong in my binding? I can do the following:

    void _Contacts_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        OnPropertyChanged("Contacts");
    }

Which basically raises the PropertyChanged event of the collection. This forces wpf to rebind, but I would rather not have an abundance of events flying through my code(especially the unnecessary ones).

Any ideas?

+1  A: 

The problem is that while you raise a property changed for Contacts, you do not raise an event for the Count property..

You can solve this with

OnPropertyChanged("Count")

in your list, since your list implements the INotifyPropertyChanged interface...

Arcturus
thank you, I didn't notice that oversight.
Jose
+1  A: 

Alternatively, you can derive from ObservableCollection instead. It has all the change notification code built into it and could save you some time in the long run.

SergioL
Problem is I added some properties to the collection class that I need for databinding.
Jose
The ObservableCollection is built for databinding. Additionally, you can easily derive from it to add additional functionality if needed.
SergioL