I have a ListBox on my WP 8.1 Silverlight app that looks like this:
<StackPanel Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Top">
<ListBox x:Name="FavoriteListBox" Visibility="Collapsed"
SelectionChanged="FavoriteListBox_SelectionChanged"
HorizontalAlignment="Stretch"
VerticalAlignment="Top" Opacity="1"
Background="{StaticResource PhoneBackgroundBrush}" Foreground="{StaticResource PhoneForegroundBrush}"
Height="300" Width="250">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Visibility="Visible" x:Name="FavoriteListBoxTextBlock"
FontSize="35" Foreground="Black" Text="{Binding AnswerName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
In my ListBox the items should look like this:
- item1
- item2
- to 15.....
However currently it is not in the order of 1,2,3,4... Instead it is in order of when the item is added.
I want the ListBox items to serialize automatically. How can this be achieved?
If you want to sort a list or array before serializing, you can use two different solutions.
Arrays have a static
Array.Sortmethod, that sorts the items of the array in-place (sorts the contents of the instance itself).For lists and other collections you can use LINQ's
OrderByextension method.Note, that the original
listis left intact, the result of theToList()extension method is a new instance.