private void Button_Click(object sender, RoutedEventArgs e) { SongItem data = (sender as Button).DataContext as SongItem; Debug.WriteLine("View song: " + data.Name); MostRecentlyViewed = data; NavigationService.Navigate(new Uri("/ViewSong.xaml?song=" + data.Name + "&path=" + data.Filepath, UriKind.Relative)); txtFilter.Text = STR_FILTER_DEFAULT; //Clear the filter when the user selects a song }
//Comparator for SongItems (alphabetical based on underlying Name field) public static int CompareSongs(SongItem a, SongItem b) { //a is greater = 1, b is greater = -1, equal = 0 return(a.Name.CompareTo(b.Name)); }
void PopulateListBasedOnFilter(String _filter = "") //default = no filter { if (_filter.Equals(STR_FILTER_DEFAULT)) //Don't filter based on the default instruction text { _filter = ""; } List <SongItem> songs = new List <SongItem>(); var test = Assembly.GetExecutingAssembly().GetManifestResourceNames(); foreach (string key in Assembly.GetExecutingAssembly().GetManifestResourceNames()) { if (!key.ToLower().Contains(".gif")) //Throw out the other (system) resources we get from the assembly { continue; } if ((_filter.Equals("")) || (SongItem.ParseSongName(key).ToLower().Contains(_filter.ToLower()))) { songs.Add(new SongItem(key)); //SongItem constructor will parse out the title } } //If there's no filter, we don't need to sort further, and can update the UI and halt here if (_filter.Equals("")) { songs.Sort(SongItem.CompareSongs); SongList.ItemsSource = songs; //Update UI //SongList.UpdateLayout(); //SongList.SelectedIndex = 0; //SongList.UpdateLayout(); return; } //Now, we want the songs that start with the filter text to appear first. //Let's strip them out of the list, sort the remaining items, then combine lists and pass to the UI List <SongItem> BestResults = new List <SongItem>(); List <SongItem> OtherResults = new List <SongItem>(); //Need to create 2 new lists because we can't modify a collection while iterating if (!_filter.Equals("")) { foreach (SongItem s in songs) { if (s.Name.ToLower().StartsWith(_filter.ToLower())) { BestResults.Add(s); } else { OtherResults.Add(s); } } } //Sort both alphabetically BestResults.Sort(SongItem.CompareSongs); OtherResults.Sort(SongItem.CompareSongs); //Concat the lists BestResults.AddRange(OtherResults); SongList.ItemsSource = BestResults; //Update UI if (BestResults.Count == 0) { //No songs found - Display message txtNotFound1.Visibility = System.Windows.Visibility.Visible; txtNotFound2.Visibility = System.Windows.Visibility.Visible; txtNotFound3.Visibility = System.Windows.Visibility.Visible; } else { txtNotFound1.Visibility = System.Windows.Visibility.Collapsed; txtNotFound2.Visibility = System.Windows.Visibility.Collapsed; txtNotFound3.Visibility = System.Windows.Visibility.Collapsed; SongList.UpdateLayout(); SongList.SelectedIndex = 0; SongList.UpdateLayout(); UpdateLayout(); } SongList.UpdateLayout(); }