I want to ensure, that the items added in my list box are ordered in ascending order according to the serial number of each item (e.g 1 item, 2 item, 4 item, 3 item should be automatically order according to its number 1.2.3.......10).
Here is the C# source:
namespace XeroQuiz
{
public partial class MainPage : PhoneApplicationPage
{
IsolatedStorageFile Settings1 = IsolatedStorageFile.GetUserStoreForApplication();
MyDataList listobj = new MyDataList();
public MainPage()
{
InitializeComponent();
this.Loaded += MainPage_Loaded;
this.FavoriteListBox.Visibility = Visibility.Collapsed;
if (Settings1.FileExists("MyStoreItems"))
{
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
listobj = (MyDataList)serializer.ReadObject(fileStream);
}
}
FavoriteListBox.ItemsSource = listobj;//binding isolated storage list data
DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.5) };
timer.Tick += delegate (object sender, EventArgs e)
{
// var sortedList = listobj.OrderBy(item => item.AnswerName).ToList(); code is correct but no item is sorted in list.
this.FavoriteListBox.ItemsSource = listobj;
this.FavoriteListBox.UpdateLayout();
};
timer.Start();
}
/**************************************************************************/
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if (Settings1.FileExists("MyStoreItems"))//loaded previous items into list
{
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
listobj = (MyDataList)serializer.ReadObject(fileStream);
}
}
}
private void FavoriteButton_Click(object sender, RoutedEventArgs e)
{
if (listobj.Any(l => l.AnswerName == AnswerTextBlock.Text))
return;
//var sortedList = listobj.OrderBy(item => item.ToString()).ToList();
listobj.Add(new MyData { AnswerName = AnswerTextBlock.Text });
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
serializer.WriteObject(fileStream, listobj);
}
}
private void FavoriteRemoveButton_Click(object sender, RoutedEventArgs e)
{
lsitobj.Remove(listobj.FirstOrDefault(l => l.AnswerName == AnswerTextBlock.Text));
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList));
serializer.WriteObject(fileStream, listobj);
}
}
}
private void FavoriteListButton_Click(object sender, RoutedEventArgs e)
{
if (FavoriteListBox.Visibility.Equals(Visibility.Collapsed))
{
FavoriteListBox.Visibility = Visibility.Visible;
}
else if (FavoriteListBox.Visibility.Equals(Visibility.Visible))
{
FavoriteListBox.Visibility = Visibility.Collapsed;
}
}
public class MyData
{
public string AnswerName { get; set; }
}
public class MyDataList : ObservableCollection<MyData>//for storing mydata class items with type of list
{
}
private void FavoriteListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MyData selecteddata = (sender as ListBox).SelectedItem as MyData;
if (selecteddata != null)
{
FavoritedData.Text = selecteddata.FrequencyName.ToString();
using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MySelectedStoreItem", FileMode.Create))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(MyData));
serializer.WriteObject(fileStream, selecteddata);
}
}
}
}
Here is the XAML code:
<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>
<StackPanel Grid.Row="0" Grid.Column= "0" HorizontalAlignment= "Left" VerticalAlignment= "Top" >
< Button x:Name= "FavoriteButton" FontFamily= "Segoe MDL2 Assets"
Content= "" BorderBrush= "Transparent" FontSize= "28"
Foreground= "{StaticResource PhoneForegroundBrush}"
Style= "{StaticResource ButtonStyle1}" Click= "FavoriteButton_Click" />
</ StackPanel >
<StackPanel Grid.Row="0" Grid.Column= "2" HorizontalAlignment= "Left" VerticalAlignment= "Top" >
< Button x:Name= "FavoriteListButton" FontFamily= "Segoe MDL2 Assets"
Content= "" BorderBrush= "Transparent" FontSize= "28"
Foreground= "{StaticResource PhoneForegroundBrush}"
Style= "{StaticResource ButtonStyle1}" Click= "FavoriteListButton_Click" />
</StackPanel>