问题
My issue goes like that:
I have 2 ListBox:
one is bound to a collection called photos of type Photos and the other is bound to a CollectionViewSource which is bound to the same photos collection.
listBox1 -> photos (here the listBox1 is bound to the Default CollectionView of photos, of course and not directly to photos.)
ListBox2 -> cvs -> photos
both collection (the default one and my CVS) having a filter that reduce the items they show from 8 items to 5 items.
now i have 2 labels. one is bound to the Count property of the photos object and the other one is bound to the Count property where the source is the cvs (my CollectionViewSource) object.
the first label show the number 8 and as i see it, it is because the Count of photos stays 8 even though i am filterring it's default CollectionViewSource.
the second label shows the number 5.
what i learnt about binding to a cvs is that WPF unwrapps the source object from the cvs and the Path=Count is relevant to the underlying object which is photos and the number here should be 8 also.
does someone can explain me where i am worng?
thanks!
回答1:
The binding will not bind to the source collection but the view. The collection view also has a Count property which returns the number of items in the (filtered) view rather than in the original collection.
(You can use the debugger to see that the View property of the CollectionViewSource normally will be an object which is an instance of CollectionView or one of its subclasses. The binding will implicitly bind to View.Count
)
To bind to the original count use the path SourceCollection.Count
.
来源:https://stackoverflow.com/questions/6722415/binding-to-a-collectionviewsource-question