MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. using WpfAppA.Windows;
  18. namespace WpfAppA
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window, INotifyPropertyChanged
  24. {
  25. public event PropertyChangedEventHandler PropertyChanged;
  26. private void Invalidate()
  27. {
  28. if (PropertyChanged != null)
  29. PropertyChanged(this, new PropertyChangedEventArgs("HeroList"));
  30. {
  31. InitializeComponent();
  32. Style buttonStyle = new Style();
  33. buttonStyle.Setters.Add(
  34. new Setter
  35. {
  36. Property = Control.FontFamilyProperty,
  37. Value = new FontFamily("Verdana")
  38. });
  39. buttonStyle.Setters.Add(
  40. new Setter
  41. {
  42. Property = Control.MarginProperty,
  43. Value = new Thickness(10)
  44. });
  45. buttonStyle.Setters.Add(
  46. new Setter
  47. {
  48. Property = Control.BackgroundProperty,
  49. Value = new SolidColorBrush(Colors.Black)
  50. });
  51. buttonStyle.Setters.Add(
  52. new Setter
  53. {
  54. Property = Control.ForegroundProperty,
  55. Value = new SolidColorBrush(Colors.White)
  56. });
  57. buttonStyle.Setters.Add(
  58. new Setter
  59. {
  60. Property = Control.MarginProperty,
  61. Value = new Thickness(10)
  62. });
  63. new EventSetter
  64. {
  65. Event = Button.ClickEvent,
  66. Handler = new RoutedEventHandler(Button_Click)
  67. };
  68. button1.Style = buttonStyle;
  69. button2.Style = buttonStyle;
  70. }
  71. }
  72. public string selectedPower = "Мощь";
  73. public HeroAge? selectedAge = null;
  74. public string selectedRace = "Раса";
  75. private IEnumerable<Hero> _HeroList;
  76. public IEnumerable<Hero> HeroList
  77. {
  78. get
  79. {
  80. var res = _HeroList;
  81. res = res
  82. .Where(c => (c.power == selectedPower || selectedPower == "Мощь"))
  83. .Where(c => (selectedAge == null || (c.Age >= selectedAge.AgeFrom && c.Age < selectedAge.AgeTo)))
  84. .Where(c => (c.race == selectedRace || selectedRace == "Раса"));
  85. if (searchFilter != "")
  86. res = res.Where(c => c.name.IndexOf(
  87. searchFilter,
  88. StringComparison.OrdinalIgnoreCase) >= 0);
  89. if (sortAsc) res = res.OrderBy(c => c.Age);
  90. else res = res.OrderByDescending(c => c.Age);
  91. return res;
  92. }
  93. set
  94. {
  95. _HeroList = value;
  96. }
  97. }
  98. public List<HeroPower> HeroPowerList { get; set; }
  99. public List<HeroAge> HeroAgeList { get; set; }
  100. public List<HeroRace> HeroRaceList { get; set; }
  101. public MainWindow()
  102. {
  103. InitializeComponent();
  104. DataContext = this;
  105. Globals.dataProvider = new JSONDataProvider();
  106. HeroList = Globals.dataProvider.getHero();
  107. HeroPowerList = Globals.dataProvider.getPower().ToList();
  108. HeroPowerList.Insert(0, new HeroPower { title = "Мощь" });
  109. HeroAgeList = Globals.dataProvider.getAge().ToList();
  110. HeroRaceList = Globals.dataProvider.getRace().ToList();
  111. HeroRaceList.Insert(0, new HeroRace { title = "Раса" });
  112. }
  113. private void ExitButton_Click(object sender, RoutedEventArgs e)
  114. {
  115. Application.Current.Shutdown();
  116. }
  117. private void HeroListBox_MouseDoubleClick(
  118. object sender,
  119. MouseButtonEventArgs e)
  120. {
  121. // в создаваемое окно передаем выбранного котика
  122. var detailWindow = new DetailWindow(
  123. HeroListBox.SelectedItem as Hero);
  124. detailWindow.ShowDialog();
  125. }
  126. private void powerFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  127. {
  128. selectedPower = (powerFilterComboBox.SelectedItem as HeroPower).title;
  129. Invalidate();
  130. }
  131. private void AgeFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  132. {
  133. selectedAge = AgeFilterComboBox.SelectedItem as HeroAge;
  134. Invalidate();
  135. }
  136. private void RaceFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  137. {
  138. selectedRace = (RaceFilterComboBox.SelectedItem as HeroRace).title;
  139. Invalidate();
  140. }
  141. private string searchFilter = "";
  142. private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
  143. {
  144. searchFilter = SearchFilterTextBox.Text;
  145. Invalidate();
  146. }
  147. private bool sortAsc = true;
  148. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  149. {
  150. sortAsc = (sender as RadioButton).Tag.ToString() == "1";
  151. Invalidate();
  152. }
  153. private void Button_Click(object sender, RoutedEventArgs e)
  154. {
  155. Button clickedButton = (Button)sender;
  156. MessageBox.Show(clickedButton.Content.ToString());
  157. }
  158. public class JSONDataProvider : LocalDataProvider, IDataProvider
  159. {
  160. private List<Hero> _HeroList;
  161. public JSONDataProvider()
  162. {
  163. var serializer = new DataContractJsonSerializer(typeof(Hero[]));
  164. using (var sr = new StreamReader("./data.json"))
  165. {
  166. _HeroList = ((Hero[])serializer.ReadObject(sr.BaseStream)).ToList();
  167. }
  168. }
  169. public IEnumerable<Hero> getHero()
  170. {
  171. return _HeroList;
  172. }
  173. }
  174. }
  175. interface IDataProvider
  176. {
  177. IEnumerable<Hero> getHero();
  178. IEnumerable<HeroPower> getPower();
  179. IEnumerable<HeroAge> getAge();
  180. IEnumerable<HeroRace> getRace();
  181. }
  182. public class LocalDataProvider : IDataProvider
  183. {
  184. public IEnumerable<HeroPower> getPower()
  185. {
  186. return new HeroPower[]
  187. {
  188. new HeroPower()
  189. {
  190. title="1/10"
  191. },
  192. new HeroPower()
  193. {
  194. title="2/10"
  195. },
  196. new HeroPower()
  197. {
  198. title="3/10"
  199. },
  200. new HeroPower()
  201. {
  202. title="4/10"
  203. },
  204. new HeroPower()
  205. {
  206. title="5/10"
  207. },
  208. new HeroPower()
  209. {
  210. title="6/10"
  211. },
  212. new HeroPower()
  213. {
  214. title="7/10"
  215. },
  216. new HeroPower()
  217. {
  218. title="8/10"
  219. },
  220. new HeroPower()
  221. {
  222. title="9/10"
  223. },
  224. new HeroPower()
  225. {
  226. title="10/10"
  227. },
  228. };
  229. }
  230. public IEnumerable<HeroAge> getAge()
  231. {
  232. return new HeroAge[]
  233. {
  234. new HeroAge()
  235. {
  236. title="Все возраста",
  237. AgeFrom=0,
  238. AgeTo=2000
  239. },
  240. new HeroAge()
  241. {
  242. title="Дети",
  243. AgeFrom=1,
  244. AgeTo=100
  245. },
  246. new HeroAge()
  247. {
  248. title="Подростки",
  249. AgeFrom=100,
  250. AgeTo=500
  251. },
  252. new HeroAge()
  253. {
  254. title="Молодые",
  255. AgeFrom=500,
  256. AgeTo=700
  257. },
  258. new HeroAge()
  259. {
  260. title="Старики",
  261. AgeFrom=700,
  262. AgeTo=2000
  263. }
  264. };
  265. }
  266. public IEnumerable<HeroRace> getRace()
  267. {
  268. return new HeroRace[]
  269. {
  270. new HeroRace()
  271. {
  272. title="Человек"
  273. },
  274. new HeroRace()
  275. {
  276. title="Дракон"
  277. },
  278. new HeroRace()
  279. {
  280. title="Фея"
  281. },
  282. new HeroRace()
  283. {
  284. title="Огр"
  285. },
  286. new HeroRace()
  287. {
  288. title="Рыба"
  289. },
  290. new HeroRace()
  291. {
  292. title="Селёдка"
  293. },
  294. new HeroRace()
  295. {
  296. title="Камень"
  297. },
  298. new HeroRace()
  299. {
  300. title="Гоблин"
  301. },
  302. };
  303. }
  304. public IEnumerable<Hero> getHero()
  305. {
  306. return new Hero[]{
  307. new Hero{
  308. race="Человек",
  309. name="Invoker",
  310. Age= 1000,
  311. power="10/10"},
  312. new Hero{
  313. race="Человек",
  314. name="Juggernaut",
  315. Age= 450,
  316. power="9/10"},
  317. new Hero{
  318. race="Человек",
  319. name="lina",
  320. Age = 300,
  321. power="8/10"},
  322. new Hero{
  323. race="Огр",
  324. name="Troll Warloard",
  325. Age = 100,
  326. power="7/10"},
  327. new Hero{
  328. race="Рыба",
  329. name="Slark",
  330. Age = 120,
  331. power="6/10"},
  332. new Hero{
  333. race="Селёдка",
  334. name="Slardar",
  335. Age = 200,
  336. power="5/10"},
  337. new Hero{
  338. race="Камень",
  339. name="Tiny",
  340. Age = 20000,
  341. power="4/10"},
  342. new Hero{
  343. race="Фея",
  344. name="Dark Willow",
  345. Age = 700,
  346. power="3/10"},
  347. new Hero{
  348. race="Огр",
  349. name="Orge Magi",
  350. Age = 1400,
  351. power="2/10"},
  352. new Hero{
  353. race="Дракон",
  354. name="Viper",
  355. Age = 1900,
  356. power="1/10"},
  357. };
  358. }
  359. }
  360. }