private void UnitSelection(object sender, SelectionChangedEventArgs e) { if ((sender as ComboBox).SelectedItem != null) { UnitPropertis selectedUnit = (sender as ComboBox).SelectedItem as UnitPropertis; FoodUnitBox.Text = selectedUnit.ToString(); } }
private void SaveStoreItem(object sender, RoutedEventArgs e) { if (FoodBox.SelectedItem == null) { MessageBox.Show("Food is not selected!"); return; } if (FoodUnitBox.SelectedItem == null) { MessageBox.Show("Unit is not selected!"); return; } if (FoodAmount.Text == "") { MessageBox.Show("Food amount field is empty!"); return; } ShopContext shopContext = new ShopContext(); string foodName = FoodBox.SelectedItem.ToString(); int foodId = shopContext.FoodDictionary.Where(f => f.Name == foodName).Select(f => f.FoodID).First(); UnitPropertis unit = FoodUnitBox.SelectedItem as UnitPropertis; List <int> unitIdList = shopContext.UnitDictionary.Where(u => u.Name == unit.Name).Select(u => u.UnitID).ToList(); int unitId = shopContext.Unit.Where(u => unitIdList.Any(d => d == u.UnitID)) .Where(u => u.UnitStep == unit.Step).Select(u => u.UnitID).First(); float amount = Convert.ToSingle(FoodAmount.Text); ShopStore shopStore = new ShopStore(); shopStore.ShopID = ShopId; shopStore.FoodID = foodId; shopStore.Amount = amount; shopStore.UnitID = unitId; if (shopContext.ShopStore.Where(s => s.FoodID == shopStore.FoodID && s.UnitID == shopStore.UnitID).Count() > 0) { MessageBox.Show("Element is existed!"); return; } shopContext.ShopStore.Add(shopStore); shopContext.SaveChanges(); FoodTypeBox.SelectedItem = null; FoodBox.SelectedItem = null; FoodUnitBox.SelectedItem = null; FoodTypeBox.Text = "Type"; FoodBox.Text = "Food"; FoodUnitBox.Text = "Unit"; FoodAmount.Text = ""; }