TestsCatalog.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace MyTests.Pages
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для TestsCatalog.xaml
  19. /// </summary>
  20. public partial class TestsCatalog : Page
  21. {
  22. public TestsCatalog()
  23. {
  24. InitializeComponent();
  25. //LoadingTests();
  26. }
  27. private void AddTest_Click(object sender, RoutedEventArgs e)
  28. {
  29. }
  30. private void AddTest(string author, BitmapImage imageSource, int test)
  31. {
  32. Grid testGrid = new Grid
  33. {
  34. Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x40, 0x44, 0x4B)),
  35. Height = 45,
  36. Width = 590,
  37. Margin = new Thickness(10, 5, 10, 5)
  38. };
  39. Image testImage = new Image
  40. {
  41. Source = imageSource,
  42. Width = 35,
  43. Height = 35,
  44. Margin = new Thickness(5),
  45. HorizontalAlignment = HorizontalAlignment.Left
  46. };
  47. testGrid.Children.Add(testImage);
  48. Label authorLabel = new Label();
  49. authorLabel.Content = author;
  50. authorLabel.Foreground = Brushes.White;
  51. authorLabel.FontWeight = FontWeights.Bold;
  52. authorLabel.HorizontalAlignment = HorizontalAlignment.Left;
  53. authorLabel.VerticalAlignment = VerticalAlignment.Top;
  54. authorLabel.Margin = new Thickness(40, 0, 0, 0);
  55. testGrid.Children.Add(authorLabel);
  56. Label testLabel = new Label();
  57. testLabel.Content = test;
  58. testLabel.Foreground = Brushes.White;
  59. testLabel.HorizontalAlignment = HorizontalAlignment.Left;
  60. testLabel.VerticalAlignment = VerticalAlignment.Bottom;
  61. testLabel.Margin = new Thickness(40, 0, 0, 0);
  62. testGrid.Children.Add(testLabel);
  63. TestsListBox.Items.Add(testGrid);
  64. }
  65. void LoadingTests()
  66. {
  67. TestsListBox.Items.Clear();
  68. var list = cnt.db.Tests.Where(item => item.IdUser == Session.userId).ToList();
  69. if(TestNameBox.Text != "Название теста")
  70. list = list.Where(item => item.Name == TestNameBox.Text).ToList();
  71. if(AuthorTestBox.Text != "Автор")
  72. list = list.Where(item => item.Users.Login == AuthorTestBox.Text).ToList();
  73. foreach (Tests test in list)
  74. {
  75. try
  76. {
  77. BitmapImage img = test.Image == null ?
  78. new BitmapImage(new Uri("../Resources/Approval.png", UriKind.RelativeOrAbsolute)) :
  79. ImagesManip.NewImage(cnt.db.Users.Where(item => item.IdUser == Session.userId).FirstOrDefault());
  80. AddTest(test.Name, img, cnt.db.Questions.Where(item => item.IdTest == test.IdTest).Count());
  81. }
  82. catch (Exception ex)
  83. {
  84. new ErrorWindow(ex.ToString()).ShowDialog();
  85. }
  86. }
  87. }
  88. private void FindTests_Click(object sender, RoutedEventArgs e)
  89. {
  90. LoadingTests();
  91. }
  92. }
  93. }