Why won't my UI-binding update?

EasyGoingPatEasyGoingPat GBMember ✭✭✭

I have a view-model (set as the DataContext for the Page) with a property that is passed through from the model layer. Something like this:

public ModelClass
{
    public ModelProperty { get { return _modeProperty; } set { _modelProperty = value; } }
    private _modelProperty;
}

public ViewModel
{
    ModelProperty MyBoundProperty { get { return ModelClassInstance.ModelProperty; } }
}

Code inside ModelClass is responsible for changing _modelProperty. When it does this, it raises an event, which is picked up by the ViewModel, which responds by doing the following:

    private void ModelChangedEventHandler( object sender, EventArgs e )
    {
        OnPropertyChanged( null );
    }

The UI does not pick up the change. Can anyone suggest why? I thought raising OnPropertyChanged() with a null parameter was like saying to the UI, "Something has changed; re-evaluate all bound properties."

Best Answers

  • MichaelRumplerMichaelRumpler AT ✭✭✭✭✭
    Accepted Answer

    You have to pass it the name of the property which changed.

    OnPropertyChanged("ModelProperty");
    

    Then the view which binds to ModelProperty knows to update.

Answers

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭
    Accepted Answer

    You have to pass it the name of the property which changed.

    OnPropertyChanged("ModelProperty");
    

    Then the view which binds to ModelProperty knows to update.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @MichaelRumpler Thanks for that, Michael. I do have the code as you have described above. What I had done, however, was called OnPropertyChanged() from another route - i.e. not from within a SetProperty() function, so the [CallerMemberName] was getting set to a function that was not the name of a bound property. Which - surprise! surprise! - was why it wasn't working.

    PS - Don't suppose I could talk you into having a look at this related question? forums.xamarin.com/discussion/73051/how-do-i-get-a-data-bound-property-in-a-parent-page-to-update#latest

Sign In or Register to comment.