Binding to a CollectionViewSource Question

≯℡__Kan透↙ 提交于 2019-12-25 01:55:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!