xamarin forms - two way binding with a picker - why cant I update a picker from code behind?

后端 未结 2 1910
北荒
北荒 2021-01-28 22:38

My products page only shows \'Product Name\' and \'Quantity\', quantity isdisplayed/ binded to the picker.

For test purposes to get this working there is only 2 products

2条回答
  •  一生所求
    2021-01-28 23:21

    It looks like you have properly implemented INotifyPropertyChanged on your ProductModel, but now you need to subscribe to it in the constructor of the ProductModel class.

    I have stubbed up a simple implementation of what it is that you are looking for (I excluded the rest of your code so that it would be easier to digest)

    public class ProductModel : INotifyPropertyChanged {
            
            //Event
            public event PropertyChangedEventHandler PropertyChanged;
    
            //Fields
            private string _ProductName;
            private int _Quantity;
    
            //Properties
            public int Quantity {
                get { return _Quantity; }
                set {
                    _Quantity = value;
                    OnPropertyChanged();
                }
            }
    
            public string ProductName {
                get { return _ProductName; }
                set {
                    _ProductName = value;
                    OnPropertyChanged();
                }
            }
    
    
            //Constructor
            public ProductModel() {
                //Subscription
                this.PropertyChanged += OnPropertyChanged;
            }
    
            //OnPropertyChanged
            private void OnPropertyChanged(object sender, PropertyChangedEventArgs e) {
                if (e.PropertyName == nameof(Quantity)) {
                    //Do anything that needs doing when the Quantity changes here...
                }
            }
    
            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    

    Let me know if this gets you going

提交回复
热议问题