// default constructor, initializing required fields and loading the products list from the file. public ProductsViewModel() { _ProductsList = new ObservableCollection<Product>(); _selectedItem = new Product(); _ProductPrice = 0.0; loadFromXml(); addCmd = new RelayCommand(addNewItem, IsNotDirty); deleteCmd = new RelayCommand(deleteItem, IsNotDirty); closeCmd = new RelayCommand(closeApplication, IsNotDirty); saveCmd = new RelayCommand(savelist, IsNotDirty); UpdateCmd = new RelayCommand(UpdateSelected, IsNotDirty); }
/// <summary> /// getting user entered data, and making use of the view model to add the new product. /// </summary> private void addnewItem() { Product newProduct = new Product(); Console.WriteLine("enter the product name:"); newProduct.ProductName = Console.ReadLine(); Console.WriteLine("enter the price of the product:"); double productPrice; bool continueLoop = true; while (continueLoop) { string stringEntered = Console.ReadLine(); switch (stringEntered) { case "back": { continueLoop = false; newProduct = null; } break; default: if (double.TryParse(stringEntered, out productPrice)) { newProduct.ProductPrice = productPrice; continueLoop = false; } else { Console.WriteLine("enter numeric value for price"); Console.WriteLine("back: go back without adding"); } break; } } if(newProduct != null) { VM_Obj.ProductsList.Add(newProduct); } }
/// <summary> /// create a new products object and add it to the productsList. /// </summary> /// <param name="obj"></param> private void addNewItem(object obj) { Product newItem = new Product(_newProductName, _newPriceValue); //newItem.IsDirty = true; _ProductsList.Add(newItem); selectedItem = newItem; newProductName = String.Empty; newPriceValue = 0; }
/// <summary> /// update an existing product item. /// </summary> /// <param name="obj"></param> public void UpdateSelected(object obj) { IsDirty = true; ProductsList.Remove(selectedItem); Product newItem = new Product(selectedItem.ProductName, selectedItem.ProductPrice); if (_IsValue) { newItem.ProductPrice = currentValue; } else if (_IsPercentage) { newItem.ProductPrice = newItem.UpdateByPercentage(currentValue); } // little bit work around for making the observable collection to refresh on the list view. // if found a better solution to do this, shouls update this method accordingly. ProductsList.Add(newItem); selectedItem = newItem; IsDirty = false; IsValue = false; IsPercentage = false; currentValue = 0.0; }