Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. <Page
  2. xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  3. xml:lang="ru-RU"
  4. TextElement.Foreground="{DynamicResource MaterialDesignBody}"
  5. TextElement.FontWeight="Regular"
  6. TextElement.FontSize="12"
  7. TextOptions.TextFormattingMode="Ideal"
  8. TextOptions.TextRenderingMode="Auto"
  9. Background="{DynamicResource MaterialDesignPaper}"
  10. FontFamily="{DynamicResource MaterialDesignFont}"
  11. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  12. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  13. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  14. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  15. xmlns:local="clr-namespace:IgrushkiShop.Pages"
  16. xmlns:Models="clr-namespace:IgrushkiShop.Models" x:Class="IgrushkiShop.Pages.ProductsPage"
  17. mc:Ignorable="d"
  18. Title="ProductsPage">
  19.  
  20. <Page.Resources>
  21. <CollectionViewSource x:Key="productViewSource" d:DesignSource="{d:DesignInstance {x:Type Models:Product}, CreateList=True}"/>
  22. </Page.Resources>
  23.  
  24. <Grid DataContext="{StaticResource productViewSource}">
  25.  
  26.  
  27. <Button Content="Добавить" HorizontalAlignment="Left" Margin="10,0,0,10" VerticalAlignment="Bottom" Width="103" Click="Button_Click"/>
  28. <Button Content="Удалить" HorizontalAlignment="Right" Margin="0,0,118,10" Width="93" Height="32" VerticalAlignment="Bottom" Click="Button_Click_1"/>
  29. <Button Content="Изменить" HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="103" Click="Button_Click_2"/>
  30. <TextBox x:Name="SearchTb" HorizontalAlignment="Right" Height="23" Margin="0,10,10,0" TextChanged="TextBox_TextChanged" VerticalAlignment="Top" Width="144"/>
  31. <DataGrid CanUserAddRows="False" x:Name="productDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10,38,10,47" RowDetailsVisibilityMode="VisibleWhenSelected">
  32. <DataGrid.Columns>
  33. <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Наименование" Width="*" IsReadOnly="True"/>
  34. <DataGridTextColumn x:Name="costColumn" Binding="{Binding Cost}" Header="Цена" Width="*" IsReadOnly="True"/>
  35. <DataGridTextColumn x:Name="allCategsColumn" Binding="{Binding AllCategs}" Header="Категории" Width="*" IsReadOnly="True"/>
  36. <DataGridTextColumn x:Name="descriptionColumn" Binding="{Binding Description}" Header="Описание" Width="*" IsReadOnly="True"/>
  37. <DataGridTextColumn x:Name="maxSell" Binding="{Binding CostWithSell}" Header="Цена со скидкой" Width="*" IsReadOnly="True"/>
  38. </DataGrid.Columns>
  39. </DataGrid>
  40. <Label Content="Поиск:" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,9,159,0"/>
  41. </Grid>
  42. </Page>
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. using IgrushkiShop.Models;
  50. using IgrushkiShop.Views;
  51. using System;
  52. using System.Collections.Generic;
  53. using System.Collections.ObjectModel;
  54. using System.Linq;
  55. using System.Text;
  56. using System.Threading.Tasks;
  57. using System.Windows;
  58. using System.Windows.Controls;
  59. using System.Windows.Data;
  60. using System.Windows.Documents;
  61. using System.Windows.Input;
  62. using System.Windows.Media;
  63. using System.Windows.Media.Imaging;
  64. using System.Windows.Navigation;
  65. using System.Windows.Shapes;
  66.  
  67. namespace IgrushkiShop.Pages
  68. {
  69. /// <summary>
  70. /// Логика взаимодействия для ProductsPage.xaml
  71. /// </summary>
  72. public partial class ProductsPage : Page, System.ComponentModel.INotifyPropertyChanged
  73. {
  74. private ObservableCollection<Product> _Data = new ObservableCollection<Product>();
  75. public ObservableCollection<Product> Data
  76. {
  77. get => _Data;
  78. set
  79. {
  80. if (_Data != value)
  81. {
  82. _Data = value;
  83. OnPropertyChanged("Data");
  84. }
  85. }
  86. }
  87.  
  88. #region INotRel
  89. public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
  90. public void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = "") => PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(name));
  91. #endregion
  92.  
  93. public ProductsPage()
  94. {
  95. InitializeComponent();
  96. productDataGrid.ItemsSource = Data;
  97. Refresh();
  98. }
  99.  
  100. void Refresh(string text = "")
  101. {
  102. Data.Clear();
  103. if (text == "")
  104. MainWindow.Ent.Product.Select(x => x).ToList().ForEach((asd) => Data.Add(asd));
  105. else
  106. MainWindow.Ent.Product.Where(x => x.Name.ToLower().Contains(text.ToLower())).ToList().ForEach((asd) => Data.Add(asd));
  107. }
  108.  
  109. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  110. {
  111. if (string.IsNullOrWhiteSpace((sender as TextBox).Text))
  112. Refresh();
  113. else
  114. Refresh((sender as TextBox).Text);
  115. }
  116.  
  117. private void Button_Click(object sender, RoutedEventArgs e)
  118. {
  119. ProductView subs = new ProductView();
  120. subs.Closed += (asd, dsa) =>
  121. {
  122. SearchTb.Text = "&";
  123. SearchTb.Text = "";
  124. };
  125. subs.ShowDialog();
  126. }
  127.  
  128. private void Button_Click_1(object sender, RoutedEventArgs e)
  129. {
  130. if (productDataGrid.SelectedItem == null)
  131. {
  132. MessageBox.Show("Не выбран элемент для удаления!");
  133. return;
  134. }
  135. var Subject = productDataGrid.SelectedItem as Product;
  136. if (MessageBox.Show("Вы уверены что хотите удалить данный элемент?", "Внимание!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  137. {
  138. try
  139. {
  140. MainWindow.Ent.Product.Remove(Subject);
  141. MainWindow.Ent.SaveChanges();
  142. SearchTb.Text = "&";
  143. SearchTb.Text = "";
  144. }
  145. catch
  146. {
  147. MessageBox.Show("Произошла ошибка удаления!");
  148. }
  149. }
  150.  
  151. }
  152.  
  153. private void Button_Click_2(object sender, RoutedEventArgs e)
  154. {
  155. if (productDataGrid.SelectedItem == null)
  156. {
  157. MessageBox.Show("Не выбран элемент для изменения!");
  158. return;
  159. }
  160. var Subject = productDataGrid.SelectedItem as Product;
  161. ProductView subs = new ProductView(Subject);
  162. subs.Closed += (asd, dsa) =>
  163. {
  164. SearchTb.Text = "&";
  165. SearchTb.Text = "";
  166. };
  167. subs.ShowDialog();
  168. }
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement