Add remove from Cart functionality

This commit is contained in:
s459315 2022-07-27 15:43:26 +02:00
parent 4a4fc708c7
commit d6d7912c96
2 changed files with 27 additions and 1 deletions

View File

@ -67,6 +67,17 @@ namespace RMWPFUserInterface.ViewModels
} }
} }
private CartItemDisplayModel _selectedCartItem;
public CartItemDisplayModel SelectedCartItem
{
get { return _selectedCartItem; }
set
{
_selectedCartItem = value;
NotifyOfPropertyChange(() => SelectedCartItem);
NotifyOfPropertyChange(() => CanRemoveFromCart);
}
}
private BindingList<CartItemDisplayModel> _cart = new BindingList<CartItemDisplayModel>(); private BindingList<CartItemDisplayModel> _cart = new BindingList<CartItemDisplayModel>();
@ -189,12 +200,27 @@ namespace RMWPFUserInterface.ViewModels
bool output = false; bool output = false;
// Make sure something is selected // Make sure something is selected
if (SelectedCartItem != null && SelectedCartItem?.Product.QuantityInStock > 0)
{
output = true;
}
return output; return output;
} }
} }
public void RemoveFromCart() public void RemoveFromCart()
{ {
SelectedCartItem.Product.QuantityInStock += 1;
if (SelectedCartItem.QuantityInCart > 1)
{
SelectedCartItem.QuantityInCart -= 1;
}
else
{
Cart.Remove(SelectedCartItem);
}
NotifyOfPropertyChange(() => SubTotal); NotifyOfPropertyChange(() => SubTotal);
NotifyOfPropertyChange(() => Tax); NotifyOfPropertyChange(() => Tax);
NotifyOfPropertyChange(() => Total); NotifyOfPropertyChange(() => Total);

View File

@ -66,7 +66,7 @@
<!-- Column 2 --> <!-- Column 2 -->
<TextBlock Text="Cart" Grid.Row="1" Grid.Column="2"/> <TextBlock Text="Cart" Grid.Row="1" Grid.Column="2"/>
<ListBox x:Name="Cart" Grid.Row="2" Grid.Column="2" <ListBox x:Name="Cart" Grid.Row="2" Grid.Column="2"
MinHeight="200" MinWidth="150"> MinHeight="200" MinWidth="150" SelectedItem="{Binding SelectedCartItem}">
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<TextBlock Text="{Binding DisplayText}"/> <TextBlock Text="{Binding DisplayText}"/>