MainWindow.xaml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 JSONDataProvider dataProvider;
  14. private InstrumentFilter filter;
  15. private string searchFilter = "";
  16. private bool sortAsc = true;
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20. dataProvider = new JSONDataProvider();
  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(dataProvider.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(dataProvider.GetInstruments()));
  35. }
  36. else
  37. {
  38. UpdateInstrumentList(filter.FilterByType(dataProvider.GetInstruments(), selectedFilter));
  39. }
  40. }
  41. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  42. {
  43. searchFilter = SearchFilterTextBox.Text;
  44. UpdateInstrumentList(dataProvider.GetInstruments());
  45. }
  46. private void RadioButtonAsc_Checked(object sender, RoutedEventArgs e)
  47. {
  48. sortAsc = true;
  49. UpdateInstrumentList(dataProvider.GetInstruments());
  50. }
  51. private void RadioButtonDesc_Checked(object sender, RoutedEventArgs e)
  52. {
  53. sortAsc = false;
  54. UpdateInstrumentList(dataProvider.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. }