Forum General

ViewModel binds not updating UI on change

BlockyNewtBlockyNewt Member ✭✭

Looked around and tried a few different things but couldn't find a solution so I though I should just ask.

I have a button and a label. What I wish to happen is for the label text to change when the button is clicked. The variable is changing, but it is not updating the UI. Here's the code...

MainPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Scaled"
                         x:Class="Scaled.MainPage">

<StackLayout>
       <Label BindingContext="{local:MainViewModel}" Text="{Binding person.Name}">
           <Button BindingContext="{local:MainViewModel}" Text="Click" Command="{Binding person.mycommand}">
<StackLayout>

</ContentPage>

Observable.cs

namespace Scaled
{
    public class Observable : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
    }
}

PersonView.cs

namespace Scaled
{
    public class PersonView : Observable
    {
        public ICommand mycommand => new Command(() => {this.Name = "hello"; });

        private string name;

        public string Name
        {
            get 
            { 
                return this.name;
            }

            set
            {
                this.name = value;
                this.OnPropertyChanged("Name");
            }
        }
    }
}

MainViewModel.cs

namespace Scaled
{
    public class MainViewModel
    {
        public PersonView person { get; private set; }

        public MainViewModel()
        {
            this.person = new PersonView();
        }
    }
}

Any kind of help will be much appreciated.

Best Answer

Answers

Sign In or Register to comment.