wpf - Sort ObservableCollection - what is the best approach? -


i have observablecollection , mydata class 4 properties i.e. int id, string name, bool isselected, string isvisible.

this observablecollection binded combobox checkboxes(cities data example). now, when user checks checkboxes next time when opens drop down - selections should come on top in ascending order name.

i have implemented auto complete when user types in 3 chars in combobox, drop down open showing selections first, then items starting 3 chars type in user.

i have researched , implemented following code , working fine, want know whether best approach or can implement in better manner, code :

        ienumerable<mydata> sort;         observablecollection<mydata> tempsortedcities = new observablecollection<mydata>();         sort = city.orderbydescending(item => item.isselected).thenby(item => item.name.toupper()) ;         // city observablecollection<mydata> property in model binded combobox in ui         foreach (var item in sort)             tempsortedcities.add(item);           city.clear(); // city observablecollection<mydata> property in model         city = tempsortedcities;         tempsortedcities = null;         sort = null;   

thanks in advance time !

icollectionview seems perfect fit this. designed sorting, filtering , grouping of collection without modifying original collection.

you can instance of icollectionview collection using following code:

var sortedcities  = collectionviewsource.getdefaultview(city); 

then can setup sorting adding instances of sortdescription type icollectionview.sortdescriptions collection:

sortedcities.sortdescriptions.add(new sortdescription("isselected", listsortdirection.descending)); sortedcities.sortdescriptions.add(new sortdescription("name", listsortdirection.ascending)); 

then can bind combobox directly collection view (instead of city collection) , display sorted data.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -