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

后端 未结 2 1911
北荒
北荒 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

    0 讨论(0)
  • 2021-01-28 23:22

    Your ProductModel class does not inherit from INotifyPropertyChanged interface, you’ll need to add the interface to your class and implement it, also making sure to raise the INotifyPropertyChanged.PropertyChanged event in Quantity setter.

    You might want to create a ProductViewModel at this point, as I’m not sure you want to add INotifyPropertyChanged to a POCO class like ProductModel.


    Please don't edit your question with a different one, open a new question. Your previous question might have helped someone else facing the same problem of UI not being updated when changing a property of a class that does not inherit from IPropertyChanged. My previous answer is now irrelevant to the new question, and will never benefit anyone else.

    For your new question, your Quantity is of type int and your picker items are of type string, changing Quantity type to string should help solving your issue, you can also use SelectedIndex instead of SelectedItem if the index of the items are matching.

    <Picker SelectedIndex="{Binding Quantity, Mode=TwoWay}">
        <Picker.Items>
            <x:String>0</x:String>
            <x:String>1</x:String>
            <x:String>2</x:String>
            <x:String>3</x:String>
            <x:String>4</x:String>
            <x:String>5</x:String>
            <x:String>6</x:String>
        </Picker.Items>
    </Picker>
    
    0 讨论(0)
提交回复
热议问题