Can a BindingContext object data binding object directly be changed on changing of an entry field?

158 views Asked by At

I am new to xamarin, i hope someone can help me with this:

I have a sinple page with entry fields and data binding.

I have page A with a listview. When I click on an item, I get redirected to page B which has the form elements.

 async void LvData_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            if (e.SelectedItem != null)
            {
                var secondPage = new ProfileDataPage();
                secondPage.BindingContext = e.SelectedItem;
                await Navigation.PushAsync(secondPage);
            }
        }

This works, and in page B the fields are filled with the right data.

So now I change the value of an entry field. Then I click on the Save Button and I do something like this (profileData = BindingContext object):

 profileData.Height = Functions.ToNullableDouble(Height.Text);
profileData.Weight = Functions.ToNullableDouble(Weight.Text);
etc...

Doesn't the BindingContext know somehow that the value of the entry has changed, and I can just send the BindingContext object to my web api for save, update and so on?

Thank you very much.

2

There are 2 answers

0
Leo Zhu On BEST ANSWER

for example,here is a mode:

class MyData : INotifyPropertyChanged
{
    string height;
    string weight;
    public MyData(string height,string weight)
    {
        this.height= height;
        this.weight= weight;
    }

    public string Height
    {
        set
        {
            if (height!= value)
            {
                height= value;
                OnPropertyChanged("Height");

            }
        }
        get
        {
            return height;
        }
    }
   public string Weight
    {
        set
        {
            if (weight!= value)
            {
                weight= value;
                OnPropertyChanged("Weight");

            }
        }
        get
        {
            return weight;
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

you could refer to Binding Mode

0
JYB On

So I tried your solutions, which helped, but only after I have update my VS, all the nuget packages and so on..

came to this idea by this post: Link

I have no idea why this was necessary, but it works now!

Thank you!