"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
The problem with your example is the OrderBy method returns an IOrderedEnumerable type of object instead of an ObservableCollection.
Up vote 5 down vote favorite 4 share g+ share fb share tw.
I have a list of data objects in my Windows Phone 7 application called MyObjectList which inherits ObservableCollection. I keep the list in memory in a public property of App called MyObjects. My goal is to bind the data to a ListBox and have it sorted by MyObject.Name.
Currently, I have a ListBox in XAML with the name MyObjectsList and the following code in the constructor to link it up: public MyObjectListView() { InitializeComponent(); this.MyObjectsList. ItemsSource = ((App)App. Current).
MyObjects; } This works great. I add items to MyObjects and they show up in the ListBox. However, the data isn't sorted by name when it appears in the list.
I tried the following change to get the data to be sorted: this.MyObjectsList. ItemsSource = ((App)App. Current).
MyObjects . OrderBy(x => x. Name) But when I do that, I don't see any objects reflected in the ListBox sorted or otherwise.
What can I do so that when I add an item to my ObservableCollection, it shows up sorted by . Name in the ListBox? C# .net silverlight windows-phone-7 link|improve this question asked Aug 24 '10 at 2:49Ben McCormack6,43012065 82% accept rate.
3 Using Linq on an ItemsSource will disable the 'Observable' bit of the ObservableCollection – Paul Betts Aug 24 '10 at 2:59.
The problem with your example is the OrderBy method returns an IOrderedEnumerable type of object instead of an ObservableCollection. Here's something you can do without implementing a custom collection like some of the other answers. Var sortedMyObjects = new ObservableCollection(); foreach (var myobj in ((App)App.
Current).MyObjects. Orderby(x => x. Name)) sortedMyObjects.
Add(myobj); this.MyObjectsList. ItemsSource = sortedMyObjects; The other answers all suggest viable alternatives, but this will solve the problem in the question. FWIW, in Silverlight 4 there is a PagedCollectionView, but Windows Phone 7's Silverlight is based on Silverlight 3 and that isn't available.
I'm only mentioning this to keep you aware of it in anticipation of WP7 eventually updating to SL4.
This worked awesomely for me! Converting all of my code to take advantage of this. – pearcewg Jan 18 '11 at 23:09 Great solution.
– peterept Mar 31 at 22:17.
You could use a sorted collection instead of your standard ObservableCollection. Someone wrote a SortedObservableCollection here: phillters.wordpress.com/2009/05/14/sorte....
This doesn't help you for Silverlight, but for WPF 3. 5/4, there's a better way to do this involving CollectionView.
Take a look at mokosh.co.uk/post/2009/08/04/how-to-sort-observablecollection/. It explains how to extend ObservableCollection to expose the underlying Items.Sort() method and then notify listeners that the collection has changed. Also, This Post here.. might help you with it.
It uses CollectionView.
The first link you referenced pointed to a great article about using . Once I figured out the right namespaces to use (always a challenge with XAML), it worked perfectly. Thanks!
– Ben McCormack Aug 24 '10 at 3:38.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.