0

I am creating a list of scanned items. Whenever an item is scanned it checks if it already exists and if it does it increases the quantity, otherwise it will be added as a new item in the list. When an item is added in the list the change is reflected correctly, however, when only the quantity is changed the value is not visually updated.

if(PurchaseUnloadData.scannedItems.Where(x => x.ItemID == item.ItemID).Count() > 0)
{
    PurchaseUnloadData.scannedItems.FirstOrDefault(x => x.ItemID == item.ItemID).Quantity = PurchaseUnloadData.scannedItems.FirstOrDefault(x => x.ItemID == item.ItemID).Quantity + 1;
}
else
{
     item.Quantity = 1;
     PurchaseUnloadData.scannedItems.Add(item);
}
MessagingCenter.Send<ViewPurchaseUnloadingCart>(this, "ItemsChanged");

In addition when i scan an item twice, meaning the quantity gets to 2, if i add a new row the quantity value is updated! If there isn't a new row it just updates the data behind but not the list.

public class PurchaseUnloadingCartViewModel : ObservableObject
{
    private NavigationCategoryData _category;
    private PurchaseUnloadData.ScannedItemInfo _selectedItem;

    public PurchaseUnloadingCartViewModel()
        : base(listenCultureChanges: true)
    {
        LoadData();

        ExportToExcelCommand = new Command(async () => await ExportDataToExcelAsync());

        MessagingCenter.Subscribe<ViewPurchaseUnloadingCart>(this, "ItemsChanged", (sender) =>
        {
            LoadData();
        });
    }

    public ObservableCollection<PurchaseUnloadData.ScannedItemInfo> Items { get; } = new ObservableCollection<PurchaseUnloadData.ScannedItemInfo>();

    public NavigationCategoryData Category
    {
        get { return _category; }
        set { SetProperty(ref _category, value); }
    }

    public PurchaseUnloadData.ScannedItemInfo SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (SetProperty(ref _selectedItem, value) && value != null)
            {
                Int64 itemID = Convert.ToInt64(value.ItemID);
                Application.Current.MainPage.Navigation.PushAsync(new AddScannedItem("EDIT", itemID));
                SetProperty(ref _selectedItem, null);
            }
        }
    }

    protected override void OnCultureChanged(CultureInfo culture)
    {
        LoadData();
    }

    private void LoadData()
    {
        Category = null;
        Items.Clear();
        int i = 0;
        while (i < PurchaseUnloadData.scannedItems.Count)
        {
            Items.Add(PurchaseUnloadData.scannedItems[i]);
            i++;
        }
    }
}
Frosty
  • 39
  • 1
  • 7