/// <summary> /// Window containing all products in Products table /// </summary> public ProductsWindow() { InitializeComponent(); db = new CaloriesEntities(); db.Products.Load(); ProductsLV.DataContext = db.Products.Local; }
/// <summary> /// Constructor of Window for adding new List /// </summary> public AddListWindow() { InitializeComponent(); list = new Lists { Creation_date = DateTime.Now }; prodList = new ObservableCollection <ProductsLists>(); db = new CaloriesEntities(); db.Products.Load(); ProductsListsLV.DataContext = prodList; ProductsLVS.DataContext = db.Products.Local; }
/// <summary> /// Start window with listview showing products lists /// </summary> public MainWindow() { InitializeComponent(); Thread oTh = new Thread(new ThreadStart(() => { db = new CaloriesEntities(); db.Lists.Load(); Dispatcher.Invoke(() => { ProductsListView.DataContext = db.Lists.Local; }); })) { IsBackground = true }; if (!oTh.IsAlive) { oTh.Start(); } }
private void AddButton_Click(object sender, RoutedEventArgs e) { Regex nameRegex = new Regex(namePattern), numberRegex = new Regex(numberPattern); if (nameRegex.IsMatch(pNameTextBox.Text) && numberRegex.IsMatch(pWeightTextBox.Text) && numberRegex.IsMatch(pCaloriesTextBox.Text)) { using (CaloriesEntities db = new CaloriesEntities()) { if (_product.Id != 0) { var result = db.Products.SingleOrDefault(b => b.Id == _product.Id); if (result != null) { result.Name = pNameTextBox.Text; try { result.weight = Int32.Parse(pWeightTextBox.Text); result.calories = Int32.Parse(pCaloriesTextBox.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } else { _product.Name = pNameTextBox.Text; try { _product.weight = Int32.Parse(pWeightTextBox.Text); _product.calories = Int32.Parse(pCaloriesTextBox.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } db.Products.Add(_product); try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } this.DialogResult = true; } else { SystemSounds.Hand.Play(); } }