c# - WPF Multiple CollectionView with different filters on same collection -
i'm using observablecollection
2 icollectionview
different filters.
one filtering messages type, , 1 counting checked messages. can see message filter , message count works ok, when i'm un-checking message disappear list (the count still working).
btw sorry long post, wanted include relevant stuff.
the xaml code:
<!-- messages list --> <dockpanel grid.row="1" grid.column="0" grid.columnspan="3" height="500"> <listbox name="listboxzone" itemssource="{binding filteredmessagelist}" background="transparent" borderthickness="0"> <listbox.itemtemplate> <datatemplate> <checkbox name="checkboxzone" content="{binding text}" tag="{binding id}" unchecked="checkboxzone_unchecked" foreground="whitesmoke" margin="0,5,0,0" ischecked="{binding ischecked}" /> </datatemplate> </listbox.itemtemplate> </listbox> </dockpanel> <button content="test add new" grid.column="2" height="25" horizontalalignment="left" margin="34,2,0,0" click="button1_click" /> <label content="{binding checkedmessagelist.count}" grid.column="2" height="25" margin="147,2,373,0" width="20" foreground="white" />
screenshot:
code:
/* viewmodel class */ public class mainviewmodel : inotifypropertychanged { // constructor public mainviewmodel() { #region filteredmessagelist // connect observablecollection collectionview _filteredmessagelist = collectionviewsource.getdefaultview(messagelist); // set filter _filteredmessagelist.filter = delegate(object item) { messageclass temp = item messageclass; if ( selectedfilter.equals(availablefilters.all) ) { return true; } else { return temp.filter.equals(_selectedfilter); } }; #endregion #region checkedmessagelist // connect observablecollection collectionview _checkedmessagelist = collectionviewsource.getdefaultview(messagelist); // set filter _checkedmessagelist.filter = delegate(object item) { return (item messageclass).ischecked; }; #endregion } // message list private observablecollection<messageclass> _messagelist = new observablecollection<messageclass>(); public observablecollection<messageclass> messagelist { { return _messagelist; } set { _messagelist = value; } } // collectionview (filtered messagelist) private icollectionview _filteredmessagelist; public icollectionview filteredmessagelist { { return _filteredmessagelist; } } // collectionview (filtered messagelist) private icollectionview _checkedmessagelist; public icollectionview checkedmessagelist { { return _checkedmessagelist; } } // selectedfilter property private availablefilters _selectedfilter = availablefilters.all; // default set public availablefilters selectedfilter { { return _selectedfilter; } set { _selectedfilter = value; raisepropertychanged("selectedfilter"); _filteredmessagelist.refresh(); // refresh list upon update } } // filterlist (convert enum collection) private list<keyvaluepair<string, availablefilters>> _availablefilterslist; public list<keyvaluepair<string, availablefilters>> availablefilterslist { { /* check if such list available, if not create first use */ if (_availablefilterslist == null) { _availablefilterslist = new list<keyvaluepair<string, availablefilters>>(); foreach (availablefilters filter in enum.getvalues(typeof(availablefilters))) { string description; fieldinfo fieldinfo = filter.gettype().getfield(filter.tostring()); descriptionattribute[] attributes = (descriptionattribute[])fieldinfo.getcustomattributes(typeof(descriptionattribute), false); /* if not null description */ if (attributes != null && attributes.length > 0) { description = attributes[0].description; } else { description = string.empty; } /* add new item filterlist */ keyvaluepair<string, availablefilters> typekeyvalue = new keyvaluepair<string, availablefilters>(description, filter); _availablefilterslist.add(typekeyvalue); } } return _availablefilterslist; } } #region implement inotifypropertychanged public event propertychangedeventhandler propertychanged; public void raisepropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } #endregion }
code un-check function
private void checkboxzone_unchecked(object sender, routedeventargs e) { checkbox chkzone = (checkbox)sender; ucsystemmessagevm.checkedmessagelist.refresh(); }
this answer helped me exact problem. static collectionviewsource.getdefaultview(coll)
method return same reference given collection, basing multiple collection views on same reference counterproductive. instantiating view follows:
icollectionview filteredview = new collectionviewsource { source=messagelist }.view;
the view can filtered/sorted/grouped independently of others. can apply filtering.
i know it's been couple months , have solved problem now, ran across question when had same problem figured add answer.
Comments
Post a Comment