Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

MVVM binding between boolean View Model variable boolean property of button is not working consisten

DuraiHDuraiH USMember ✭✭
edited February 2017 in Xamarin.iOS

Change in Viewmodel variable value in shared code triggers change event in view controller respectively but after some time its not triggering the change event in view controller and in the immediate window I can see that the values of this.Vm.IsButtonEnabled and this.myButton.Enabled is different how to fix this unsynchronised binding.

In Viewcontroller viewdidload

bindings.Add(this.SetBinding(() => this.Vm.IsButtonEnabled,()=> this.myButton.Enabled,BindingMode.TwoWay));

bindings.Add(this.SetBinding(() => this.Vm.IsButtonEnabled).WhenSourceChanges(() => {
                if (this.Vm.IsButtonEnabled)
                {
                    //Todo:apply enabled background color and text color
                }
                else
                {
                    //Todo:apply disabled background color and text color
                }
            }));

In ViewModel

private bool isButtonEnabled;
public bool IsButtonEnabled
        {
            get
            {
                return this.isButtonEnabled;
            }

            set
            {
                this.Set(() => this.IsButtonEnabled, ref this.isButtonEnabled, value);
            }
        }

//Calling 
this.IsButtonEnabled = true;
this.IsButtonEnabled = false;

FYI:
Note that I am not facing any problem for string variables I can see this problem happens only for Boolean variables.
Any help is appreciated

Best Answer

  • DuraiHDuraiH USMember ✭✭
    Accepted Answer

    Declaring the bindings globally at view controller solved this problem for me

    List<Binding> bindings;

    initiate the bindings variable in viewdidload

    bindings = new List<Binding>();

Answers

  • DuraiHDuraiH USMember ✭✭
    Accepted Answer

    Declaring the bindings globally at view controller solved this problem for me

    List<Binding> bindings;

    initiate the bindings variable in viewdidload

    bindings = new List<Binding>();

Sign In or Register to comment.