wpf - How to filter a collection when binding it to ItemsSource? -
i created treeview modelizes directories , files on hard drive. each treeviewitem has checkbox, binded isselected property. i'd achieve display each parent node count of selected files on total files count (10 / 12 10 files on twelve total selected).
is there way binding property ...?
<contentpresenter content="{binding myitems.count myitems.isselected, mode=onetime}" margin="2,0" />
there no way directly filter collection in binding. however, wpf allows filtering (and sorting , grouping) collections collectionviewsource
.
one approach define collectionviewsource
in resources of itemtemplate
filters itemssource
number of elements pass filter binding count property of collectionviewsource
. have define filter in codebehind, though. this:
<treeview x:name="tree" itemssource="{binding items}"> <treeview.itemtemplate> <hierarchicaldatatemplate itemssource="{binding childitems}"> <hierarchicaldatatemplate.resources> <collectionviewsource x:key="filtereditems" source="{binding childitems}" filter="filtereditems_onfilter" /> </hierarchicaldatatemplate.resources> <textblock> <textblock.text> <multibinding stringformat="{} {0} of {1} selected"> <binding path="count" source="{staticresource filtereditems}" /> <binding path="itemssource.count" elementname="tree" /> </multibinding> </textblock.text> </textblock> </hierarchicaldatatemplate> </treeview.itemtemplate> </treeview>
and in codebehind:
private void filtereditems_onfilter(object sender, filtereventargs e) { var item = sender item; return item.isselected; }
i haven't tested should work in general. never know wpf, though...
Comments
Post a Comment