开发者

Sortable Object Collection

I'm look for some direction.

I need a Sortable collection of objects which can also notify when items are added / removed from this collection as I'm binding it to a menu items as a list of windows open in my application.

Could someone please advise which would be a good collection type i.e. List<>, ObservableCollection et cetera and how I would go about sor开发者_Python百科ting the said collection.

Many Thanks in advance.


This is sortable observable collection

And here is another implementation - I use this one in my project, works flawlessly (I just had to extend it so it implements also IList interface, so it can be used to define collections in XAML).


You could try and wrap your ObservableCollection in a CollectionView, notifications will be propagated and you can sort, filter and group items.

Note that the sorting does not modify the source collection which might be a problem if you need the changes to be permanent, then again only yesterday i used the class for the first time so don't know much about it, maybe you can apply the sorting to the source somehow.


Have you tried using System.Windows.Forms.BindingSource along with a System.Windows.Forms.BindingNavigator? These will do exactly what it sounds like you need- bind a collection of objects to a navigable menu.

As for sorting, you just have to get a list of the BindingSource's items, clear the BindingSource, do your sorting on the list, and add the sorted items to the BindingSource again.

See these links for helpful examples:

  • http://msdn.microsoft.com/en-us/library/b9y7cz6d.aspx
  • http://www.code-magazine.com/article.aspx?quickid=0507051&page=2
  • http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2008-09/msg01820.html
  • http://blogs.msdn.com/b/dchandnani/archive/2005/03/09/391308.aspx


Thanks for all the responses.

Was able to simplify things as I realised I only needed the collection to be sorted for display purposes when binding to the menu items.

I was therefore able to use the following code to sort my list of panel objects via menu item parent (miPanels) in this case. (PanelName being one of the properties of the object)

 miPanels.Items.SortDescriptions.Add(new SortDescription("PanelName", ListSortDirection.Ascending));

Once again thanks for all the people who took the time to look and respond.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜