locked
I cant refresh my listview with new data in my UI when I change something. I'm using MVVM RRS feed

  • Question

  • User388951 posted

    I got a TabbedPage, this contain two pages (Products - ShoppingCart). To both of them I shared in their constructor the same instant of my viewmodel. In my fisrt page a want to search some products and add it to my second page (my shopping cart) every time I chose a product from the first page I add it on a "list" and I want that the second page could read that list and refresh the UI with the items of the list and if I add the same product I will check on the list and if it exist I'll change the count and all of that I need to reflect it in the second page when that process finish.

    Monday, November 18, 2019 6:22 AM

Answers

  • User387013 posted

    Define your *CartItem *class as follows. public class CartItem { public Product Item {get;set;} public int ItemCount {get;set;} = 1; }

    Now change AddItemToCart() as follows: public void AddItemToCart(Product newProduct) { var cartItems = Cart.ToList(); if(cartItems.Exists(x=>x.Id == newProduct.Id)) { cartItems.FirstOrDefault(x=>x.Id == newProduct.Id).ItemCount ++; } else { cartItems.Add(newItem); }

    Cart = new ObservableCollection<CartItems>(cartItems);
    OnPropertyChanged(Cart);
    

    }

    If this solves your issue then hit a like.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, November 18, 2019 8:12 AM

All replies

  • User387013 posted
        public ObservableCollection<CartItems> Cart {get; set; } = new  ObservableCollection<CartItems>();
        ....
        ....
    
        public void AddItemToCart(CartItem newItem)
        {
            var cartItems = Cart.ToList();
            cartItems.Add(newItem);
            Cart = new ObservableCollection<CartItems>(cartItems);
            OnPropertyChanged(Cart);
        }
    

    I hope this solves your problem.

    Monday, November 18, 2019 6:53 AM
  • User388951 posted

    @matrixlukan Thats worked to add an item! How can I change the "count" property of a product which is already on the list and then show it again with the new count on the UI?

    Monday, November 18, 2019 7:33 AM
  • User369978 posted

    Make your Product class implement INotifyPropertyChanged , create binding between Count and Label.Text , notify the UI while Count property changes .

     public class Product : INotifyPropertyChanged
     {
        private int count;
        // Declare the event
        public event PropertyChangedEventHandler PropertyChanged;
    
        public Product()
        {
        }
    
        public string Count
        {
            get { return count; }
            set
            {
                count = value;
                // Call OnPropertyChanged whenever the property is updated
                OnPropertyChanged("Count");
            }
        }
    
        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
    
    Monday, November 18, 2019 7:57 AM
  • User387013 posted

    Define your *CartItem *class as follows. public class CartItem { public Product Item {get;set;} public int ItemCount {get;set;} = 1; }

    Now change AddItemToCart() as follows: public void AddItemToCart(Product newProduct) { var cartItems = Cart.ToList(); if(cartItems.Exists(x=>x.Id == newProduct.Id)) { cartItems.FirstOrDefault(x=>x.Id == newProduct.Id).ItemCount ++; } else { cartItems.Add(newItem); }

    Cart = new ObservableCollection<CartItems>(cartItems);
    OnPropertyChanged(Cart);
    

    }

    If this solves your issue then hit a like.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, November 18, 2019 8:12 AM
  • User388951 posted

    @matrixlukan Thanks!!

    Wednesday, November 20, 2019 10:36 AM