MainWindow.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System.ComponentModel;
  2. using System.Text;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Data;
  6. using System.Windows.Documents;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using System.Windows.Navigation;
  11. using System.Windows.Shapes;
  12. using System.Threading.Tasks;
  13. using System;
  14. using WpfAppA.model;
  15. using System.IO;
  16. using System.Runtime.Serialization.Json;
  17. namespace WpfAppA
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window, INotifyPropertyChanged
  23. {
  24. public event PropertyChangedEventHandler PropertyChanged;
  25. private void Invalidate()
  26. {
  27. if (PropertyChanged != null)
  28. PropertyChanged(this, new PropertyChangedEventArgs("HeroList"));
  29. }
  30. public string selectedPower = "Мощь";
  31. public HeroAge? selectedAge = null;
  32. public string selectedRace = "Раса";
  33. private IEnumerable<Hero> _HeroList;
  34. public IEnumerable<Hero> HeroList
  35. {
  36. get
  37. {
  38. var res = _HeroList;
  39. res = res
  40. .Where(c => (c.power == selectedPower || selectedPower == "Мощь"))
  41. .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
  42. .Where(c => (c.race == selectedRace || selectedRace == "Раса"));
  43. if (searchFilter != "")
  44. res = res.Where(c => c.name.IndexOf(
  45. searchFilter,
  46. StringComparison.OrdinalIgnoreCase) >= 0);
  47. if (sortAsc) res = res.OrderBy(c => c.Age);
  48. else res = res.OrderByDescending(c => c.Age);
  49. return res;
  50. }
  51. set
  52. {
  53. _HeroList = value;
  54. }
  55. }
  56. public List<HeroPower> HeroPowerList { get; set; }
  57. public List<HeroAge> HeroAgeList { get; set; }
  58. public List<HeroRace> HeroRaceList { get; set; }
  59. public MainWindow()
  60. {
  61. InitializeComponent();
  62. DataContext = this;
  63. Globals.dataProvider = new JSONDataProvider();
  64. HeroList = Globals.dataProvider.getHero();
  65. HeroPowerList = Globals.dataProvider.getPower().ToList();
  66. HeroPowerList.Insert(0, new HeroPower { title = "Мощь" });
  67. HeroAgeList = Globals.dataProvider.getAge().ToList();
  68. HeroRaceList = Globals.dataProvider.getRace().ToList();
  69. HeroRaceList.Insert(0, new HeroRace { title = "Раса" });
  70. }
  71. private void ExitButton_Click(object sender, RoutedEventArgs e)
  72. {
  73. Application.Current.Shutdown();
  74. }
  75. private void powerFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  76. {
  77. selectedPower = (powerFilterComboBox.SelectedItem as HeroPower).title;
  78. Invalidate();
  79. }
  80. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  81. {
  82. selectedAge = AgeFilterComboBox.SelectedItem as HeroAge;
  83. Invalidate();
  84. }
  85. private void RaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  86. {
  87. selectedRace = (RaceFilterComboBox.SelectedItem as HeroRace).title;
  88. Invalidate();
  89. }
  90. private string searchFilter = "";
  91. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  92. {
  93. searchFilter = SearchFilterTextBox.Text;
  94. Invalidate();
  95. }
  96. private bool sortAsc = true;
  97. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  98. {
  99. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  100. Invalidate();
  101. }
  102. public class JSONDataProvider : LocalDataProvider, IDataProvider
  103. {
  104. private List<Hero> _HeroList;
  105. public JSONDataProvider()
  106. {
  107. var serializer = new DataContractJsonSerializer(typeof(Hero[]));
  108. using (var sr = new StreamReader("./data.json"))
  109. {
  110. _HeroList = ((Hero[])serializer.ReadObject(sr.BaseStream)).ToList();
  111. }
  112. }
  113. public IEnumerable<Hero> getHero()
  114. {
  115. return _HeroList;
  116. }
  117. }
  118. }
  119. interface IDataProvider
  120. {
  121. IEnumerable<Hero> getHero();
  122. IEnumerable<HeroPower> getPower();
  123. IEnumerable<HeroAge> getAge();
  124. IEnumerable<HeroRace> getRace();
  125. }
  126. public class LocalDataProvider : IDataProvider
  127. {
  128. public IEnumerable<HeroPower> getPower()
  129. {
  130. return new HeroPower[]
  131. {
  132. new HeroPower()
  133. {
  134. title="1/10"
  135. },
  136. new HeroPower()
  137. {
  138. title="2/10"
  139. },
  140. new HeroPower()
  141. {
  142. title="3/10"
  143. },
  144. new HeroPower()
  145. {
  146. title="4/10"
  147. },
  148. new HeroPower()
  149. {
  150. title="5/10"
  151. },
  152. new HeroPower()
  153. {
  154. title="6/10"
  155. },
  156. new HeroPower()
  157. {
  158. title="7/10"
  159. },
  160. new HeroPower()
  161. {
  162. title="8/10"
  163. },
  164. new HeroPower()
  165. {
  166. title="9/10"
  167. },
  168. new HeroPower()
  169. {
  170. title="10/10"
  171. },
  172. };
  173. }
  174. public IEnumerable<HeroAge> getAge()
  175. {
  176. return new HeroAge[]
  177. {
  178. new HeroAge()
  179. {
  180. title="Все возраста",
  181. AgeFrom=0,
  182. AgeTo=2000
  183. },
  184. new HeroAge()
  185. {
  186. title="Дети",
  187. AgeFrom=1,
  188. AgeTo=100
  189. },
  190. new HeroAge()
  191. {
  192. title="Подростки",
  193. AgeFrom=100,
  194. AgeTo=500
  195. },
  196. new HeroAge()
  197. {
  198. title="Молодые",
  199. AgeFrom=500,
  200. AgeTo=700
  201. },
  202. new HeroAge()
  203. {
  204. title="Старики",
  205. AgeFrom=700,
  206. AgeTo=2000
  207. }
  208. };
  209. }
  210. public IEnumerable<HeroRace> getRace()
  211. {
  212. return new HeroRace[]
  213. {
  214. new HeroRace()
  215. {
  216. title="Человек"
  217. },
  218. new HeroRace()
  219. {
  220. title="Дракон"
  221. },
  222. new HeroRace()
  223. {
  224. title="Фея"
  225. },
  226. new HeroRace()
  227. {
  228. title="Огр"
  229. },
  230. new HeroRace()
  231. {
  232. title="Рыба"
  233. },
  234. new HeroRace()
  235. {
  236. title="Селёдка"
  237. },
  238. new HeroRace()
  239. {
  240. title="Камень"
  241. },
  242. new HeroRace()
  243. {
  244. title="Гоблин"
  245. },
  246. };
  247. }
  248. public IEnumerable<Hero> getHero()
  249. {
  250. return new Hero[]{
  251. new Hero{
  252. race="Человек",
  253. name="Invoker",
  254. Age=1000,
  255. power="10/10"},
  256. new Hero{
  257. race="Человек",
  258. name="Juggernaut",
  259. Age=450,
  260. power="9/10"},
  261. new Hero{
  262. race="Человек",
  263. name="lina",
  264. Age = 300,
  265. power="8/10"},
  266. new Hero{
  267. race="Огр",
  268. name="Troll Warloard",
  269. Age = 100,
  270. power="7/10"},
  271. new Hero{
  272. race="Рыба",
  273. name="Slark",
  274. Age = 120,
  275. power="6/10"},
  276. new Hero{
  277. race="Селёдка",
  278. name="Slardar",
  279. Age = 200,
  280. power="5/10"},
  281. new Hero{
  282. race="Камень",
  283. name="Tiny",
  284. Age = 20000,
  285. power="4/10"},
  286. new Hero{
  287. race="Фея",
  288. name="Dark Willow",
  289. Age = 700,
  290. power="3/10"},
  291. new Hero{
  292. race="Огр",
  293. name="Orge Magi",
  294. Age = 1400,
  295. power="2/10"},
  296. new Hero{
  297. race="Дракон",
  298. name="Viper",
  299. Age = 1900,
  300. power="1/10"},
  301. };
  302. }
  303. }
  304. }