MainWindow.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using WpfApp2;
  8. using WpfApp3.Class;
  9. namespace WpfApp3
  10. {
  11. public partial class MainWindow : Window
  12. {
  13. private InstrumentDataManager dataManager;
  14. private InstrumentFilter filter;
  15. private string searchFilter = "";
  16. private bool sortAsc = true;
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20. dataManager = new InstrumentDataManager();
  21. filter = new InstrumentFilter();
  22. FilterComboBox.Items.Add("Все");
  23. FilterComboBox.Items.Add("Клавишные");
  24. FilterComboBox.Items.Add("Струнные");
  25. FilterComboBox.Items.Add("Ударные");
  26. FilterComboBox.SelectedIndex = 0;
  27. UpdateInstrumentList(dataManager.GetInstruments());
  28. }
  29. private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  30. {
  31. string selectedFilter = (string)FilterComboBox.SelectedItem;
  32. if (selectedFilter == "Все")
  33. {
  34. UpdateInstrumentList(filter.FilterAll(dataManager.GetInstruments()));
  35. }
  36. else
  37. {
  38. UpdateInstrumentList(filter.FilterByType(dataManager.GetInstruments(), selectedFilter));
  39. }
  40. }
  41. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  42. {
  43. searchFilter = SearchFilterTextBox.Text;
  44. UpdateInstrumentList(dataManager.GetInstruments());
  45. }
  46. private void RadioButtonAsc_Checked(object sender, RoutedEventArgs e)
  47. {
  48. sortAsc = true;
  49. UpdateInstrumentList(dataManager.GetInstruments());
  50. }
  51. private void RadioButtonDesc_Checked(object sender, RoutedEventArgs e)
  52. {
  53. sortAsc = false;
  54. UpdateInstrumentList(dataManager.GetInstruments());
  55. }
  56. private void UpdateInstrumentList(List<MusicalInstrument> instrumentsToDisplay)
  57. {
  58. if (!string.IsNullOrEmpty(searchFilter))
  59. {
  60. instrumentsToDisplay = instrumentsToDisplay.Where(i => i.Name.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0 ||
  61. i.Type.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >=0 ||
  62. i.Brand.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0).ToList();
  63. }
  64. if (sortAsc)
  65. {
  66. instrumentsToDisplay = instrumentsToDisplay.OrderBy(i => i.Price).ToList();
  67. }
  68. else
  69. {
  70. instrumentsToDisplay = instrumentsToDisplay.OrderByDescending(i => i.Price).ToList();
  71. }
  72. InstrumentListView.ItemsSource = instrumentsToDisplay;
  73. }
  74. private void ExitButton_Click(object sender, RoutedEventArgs e)
  75. {
  76. Application.Current.Shutdown();
  77. }
  78. }
  79. }