I have a list of data objects in my Windows Phone 7 application called MyObjectList, which inherits ObservableCollection<MyObject>. 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?