I have a LongListSelector
that is currently populated with images from IsolatedStorage
. I would like to be able to sort them by date in ascending or descending order. I turned to http://babaandthepigman.wordpress.com/2011/07/03/wp7-collectionviewsource-sorting-a-listbox/ for help. For some reason, I was having problems binding the ItemsSource
my LongListSelector
to the CollectionViewSource
to implement the sort function.
PictureRepository.cs (to load images from IsolatedStorage
when the application starts)
#region Constants public const string IsolatedStoragePath = "Pictures";
App.xaml.cs
public static PictureRepository PictureList { get { return PictureRepository.Instance; } }
MainPage.xaml
<phone:LongListSelector x:Name="Recent" Margin="0,0,0,72" LayoutMode="Grid" GridCellSize="108,108" SelectionChanged="recent_SelectionChanged">
MainPage.xaml.cs
#region Fields public System.Windows.Data.CollectionViewSource Source { get; set; } #endregion protected override void OnNavigatedTo(NavigationEventArgs e) { //Recent.ItemsSource = App.PictureList.Pictures; //working, unsorted Source = new System.Windows.Data.CollectionViewSource(); Source.Source = App.PictureList.Pictures; //Not Working //Need some sort of Cast to IList for LongListSelector? Recent.ItemsSource = Source.View as IList<??>(); if (Settings.AscendingSort.Value) { //Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderBy(x => x.DateTaken); //Recent.ItemsSource = App.PictureList.Pictures.OrderBy(x => x.DateTaken); Source.SortDescriptions.Clear(); Source.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Ascending)); } else { //Recent.ItemsSource = PictureRepository.Instance.Pictures.OrderByDescending(x => x.DateTaken); //Recent.ItemsSource = App.PictureList.Pictures.OrderByDescending(x => x.DateTaken); Source.SortDescriptions.Clear(); Source.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Descending)); } }
source share