public ShoppingCartEntryViewModel(ShoppingCart shoppingCart, ShoppingCartEntry entry)
        {
            _shoppingCart         = shoppingCart;
            this.ProductViewModel = new ProductViewModel(entry.Product);

            Update(entry);
        }
示例#2
0
        private void AddOrUpdateEntry(Product product, Func <ShoppingCartEntry, ShoppingCartEntry> updateFunc)
        {
            int productIndex = _shoppingCartEntries.IndexOf((entry) => (entry.Product == product));

            if (productIndex == -1)
            {
                // Create new entry.
                ShoppingCartEntry entry = new ShoppingCartEntry()
                {
                    Product  = product,
                    Quantity = 0,
                };

                // Allow update.
                entry = updateFunc(entry);

                // Add to list.
                _shoppingCartEntries.Add(entry);

                // Raise changed event.
                RaiseEntriesChangedEvent(ShoppingCartEntriesChangedType.EntryAdded, _shoppingCartEntries.Count - 1);
            }
            else
            {
                ShoppingCartEntry entry = _shoppingCartEntries[productIndex];

                // Apply update
                entry = updateFunc(entry);
                _shoppingCartEntries[productIndex] = entry;

                // Raise changed event.
                RaiseEntriesChangedEvent(ShoppingCartEntriesChangedType.EntryUpdated, productIndex);
            }
        }
        /// <summary>
        /// Updates this view model with the latest information from the shopping cart entry.
        /// </summary>
        /// <param name="entry"></param>
        public void Update(ShoppingCartEntry entry)
        {
            if (this.Product != entry.Product)
            {
                throw new Exception("Updating with wrong 'Product' entry.");
            }

            _quantityString = entry.Quantity.ToString();
            RaisePropertyChanged(nameof(QuantityString));
        }