Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Can't make Custom View Cell update on property change

$
0
0

Hi, this is my first week with Xamarin and I can't figure this out.

I have 3 classes:
1) ViewModel
2) ContentPage
3) ViewCell

And a ObservableCollection where T is the ViewModel. If I add or remove items from the collection, those changes appear in the UI. But, if I modify any item, the onchange method it's called but no changes on the UI. What is what I'm doing wrong?

Thanks.

                public class PricesPage : ContentPage
                    {
                        public ObservableCollection<StockModel> Stocks { get; set; }
                        public PricesPage()
                        {
                            Stocks = new ObservableCollection<StockModel>();
                            FillStocks();
                            var listView = new ListView();
                            listView.ItemTemplate = new DataTemplate(typeof(PricesTableCustomCell));
                            listView.ItemsSource = Stocks;
                            BindingContext = this;
                            Content = listView;
                            Xamarin.Forms.Device.StartTimer(TimeSpan.FromMilliseconds(5000), () =>
                            {
                            //   Stocks.Clear() ;
                                Stocks.Add(new StockModel() { Name = "TS", Price = new decimal(157.3), Variation = new decimal(0.03) });
                                foreach (var stock in Stocks)
                                {
                                    stock.Name += "a";
                                    stock.Price += 1;
                                }
                                return true;
                            });

                        }

                        private void FillStocks()
                        {
                            Stocks.Add(new StockModel() { Name = "TS", Price = new decimal(157.3), Variation = new decimal(0.03) });
                            Stocks.Add(new StockModel() { Name = "APBR", Price = new decimal(157.3), Variation = new decimal(0.03) });
                            Stocks.Add(new StockModel() { Name = "GGAL", Price = new decimal(157.3), Variation = new decimal(0.03) });
                            Stocks.Add(new StockModel() { Name = "COME", Price = new decimal(157.3), Variation = new decimal(0.03) });
                        }
                    }

          public class PricesTableCustomCell : ViewCell
            {
                public Label Right = new Label();
                public Label Left = new Label();
                public PricesTableCustomCell()
                {
                    var cellWrapper = new StackLayout();
                    var horizontalLayout = new StackLayout();
                    horizontalLayout.Orientation = StackOrientation.Horizontal;

                    Left.SetBinding<StockModel>(Label.TextProperty, x=> x.Name, BindingMode.TwoWay);
                    Right.SetBinding(Label.TextProperty, "Price", BindingMode.TwoWay);
                    Right.HorizontalOptions = LayoutOptions.EndAndExpand;

                    horizontalLayout.Children.Add(Left);
                    horizontalLayout.Children.Add(Right);

                    cellWrapper.Children.Add(horizontalLayout);

                    View = cellWrapper;

                }
            }

          public class StockModel : INotifyPropertyChanged
            {


                public event PropertyChangedEventHandler PropertyChanged;
                private void OnPropertyChanged(object sender, string propertyName)
                {
                    if (this.PropertyChanged != null)
                    {
                        PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
                    }
                }
                private string _name;

                public string Name
                {
                    get { return _name; }
                    set
                    {
                        if (_name != value)
                        {
                            _name = value;
                            OnPropertyChanged(this, "Stocks");    
                        }

                    }
                }
                public decimal Price { get; set; }
                public decimal Variation { get; set; }


            }

Viewing all articles
Browse latest Browse all 91519

Trending Articles