MainWindow.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Media;
  9. namespace WpfApp3
  10. {
  11. public partial class MainWindow : Window
  12. {
  13. public ObservableCollection<Student> StudentList { get; set; }
  14. private Dictionary<string, object> filters = new Dictionary<string, object>();
  15. private string currentStyle = "StackStyle";
  16. public MainWindow()
  17. {
  18. InitializeComponent();
  19. DataContext = this;
  20. Globals.dataProvider = new LocalDataProvider();
  21. StudentList = new ObservableCollection<Student>(Globals.dataProvider.GetStudents());
  22. List<string> styles = new List<string> { "light", "dark" };
  23. styleBox.SelectionChanged += ThemeChange;
  24. styleBox.ItemsSource = styles;
  25. styleBox.SelectedItem = "dark";
  26. Style buttonStyle = new Style();
  27. buttonStyle.Setters.Add(
  28. new Setter
  29. {
  30. Property = Control.FontFamilyProperty,
  31. Value = new FontFamily("Verdana")
  32. });
  33. buttonStyle.Setters.Add(
  34. new Setter
  35. {
  36. Property = Control.MarginProperty,
  37. Value = new Thickness(10)
  38. });
  39. buttonStyle.Setters.Add(
  40. new Setter
  41. {
  42. Property = Control.BackgroundProperty,
  43. Value = new SolidColorBrush(Colors.PowderBlue)
  44. });
  45. buttonStyle.Setters.Add(
  46. new Setter
  47. {
  48. Property = Control.ForegroundProperty,
  49. Value = new SolidColorBrush(Colors.Black)
  50. });
  51. }
  52. private void ExitButton_Click(object sender, RoutedEventArgs e)
  53. {
  54. Application.Current.Shutdown();
  55. }
  56. private void NameFilter_TextChanged(object sender, TextChangedEventArgs e)
  57. {
  58. filters["Name"] = ((TextBox)sender).Text;
  59. ApplyFilter();
  60. }
  61. private void NationFilter_TextChanged(object sender, TextChangedEventArgs e)
  62. {
  63. filters["Nation"] = ((TextBox)sender).Text;
  64. ApplyFilter();
  65. }
  66. private void ApplyFilter()
  67. {
  68. var filteredStudents = Globals.dataProvider.GetStudents();
  69. foreach (var filter in filters)
  70. {
  71. filteredStudents = filteredStudents.Where(s => s.GetType().GetProperty(filter.Key).GetValue(s, null).ToString().Contains(filter.Value.ToString()));
  72. }
  73. filteredStudents = filteredStudents.Where(s => s.Age >= 20 && s.Course > 2);
  74. StudentList = new ObservableCollection<Student>(filteredStudents);
  75. }
  76. private void Button_Click(object sender, RoutedEventArgs e)
  77. {
  78. MessageBoxResult result = MessageBox.Show("Семен Хомяк закончит универ ?", "Вопрос", MessageBoxButton.YesNo);
  79. if (result == MessageBoxResult.Yes)
  80. {
  81. }
  82. else
  83. {
  84. }
  85. }
  86. private void Button2_Click(object sender, RoutedEventArgs e)
  87. {
  88. MessageBoxResult result = MessageBox.Show("Сколько живет пингвин ?", "Вопрос", MessageBoxButton.YesNo);
  89. if (result == MessageBoxResult.Yes)
  90. {
  91. }
  92. else
  93. {
  94. }
  95. }
  96. private void ThemeChange(object sender, SelectionChangedEventArgs e)
  97. {
  98. string style = styleBox.SelectedItem as string;
  99. var uri = new Uri(style + ".xaml", UriKind.Relative);
  100. ResourceDictionary resourceDict =
  101. Application.LoadComponent(uri) as ResourceDictionary;
  102. Application.Current.Resources.Clear();
  103. Application.Current.Resources.MergedDictionaries.Add(resourceDict);
  104. }
  105. private void ToggleViewButton_Click(object sender, RoutedEventArgs e) {
  106. currentStyle = currentStyle == "StackStyle" ? "WrapStyle" : "StackStyle";
  107. var newStyle = (Style)TryFindResource(currentStyle);
  108. if (newStyle != null)
  109. StudentListBox.Style = newStyle;
  110. }
  111. }
  112. internal interface IDataProvider
  113. {
  114. IEnumerable<Student> GetStudents();
  115. }
  116. public class LocalDataProvider : IDataProvider
  117. {
  118. public IEnumerable<Student> GetStudents()
  119. {
  120. return new List<Student>
  121. {
  122. new Student { Id = 1, Name = "Хэй Вэнь", Age = 24, Country = "Китай", GPA = 5.0, Speciality = "Программирование", Grant = 25000.0, Course = 4, EnrollmentDate = new DateTime(2020, 9, 1), IsFullTime = true },
  123. new Student { Id = 2, Name = "Семен Хомяк", Age = 22, Country = "Россия", GPA = 3.7, Speciality = "Инженерия", Grant = 10000.0, Course = 2, EnrollmentDate = new DateTime(2022, 1, 15), IsFullTime = false },
  124. new Student { Id = 3, Name = "Джейкоб Ли", Age = 19, Country = "Америка", GPA = 4.4, Speciality = "Лингвистика", Grant = 20000.0, Course = 1, EnrollmentDate = new DateTime(2023, 8, 20), IsFullTime = true },
  125. new Student { Id = 4, Name = "Арамсунторнсук Пирапонг", Age = 18, Country = "Таиланд", GPA = 4.1, Speciality = "Логистика", Grant = 15000.0, Course = 1, EnrollmentDate = new DateTime(2023, 4, 6), IsFullTime = false },
  126. new Student { Id = 5, Name = "Рипка Дарина", Age = 18, Country = "Украина", GPA = 4.8, Speciality = "Дизайн", Grant = 23000.0, Course = 1, EnrollmentDate = new DateTime(2023, 5, 15), IsFullTime = true },
  127. };
  128. }
  129. }
  130. public class Student
  131. {
  132. public int Id { get; set; }
  133. public string Name { get; set; }
  134. public string Speciality { get; set; }
  135. public string Country { get; set; }
  136. public double GPA { get; set; }
  137. public double Grant { get; set; }
  138. public DateTime EnrollmentDate { get; set; }
  139. public bool IsFullTime { get; set; }
  140. public int Age { get; internal set; }
  141. public int Course { get; internal set; }
  142. }
  143. }