locked
ViewModel binds not updating UI on change RRS feed

  • Question

  • User396847 posted

    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 ```

    Wednesday, September 16, 2020 8:42 AM

Answers

  • User392521 posted

    Hello @BlockyNewt, Created a sample project, In this project label text updating using ViewModel on button click. I think this might help you.

    MainPage.xaml

    <StackLayout Margin="20"> <Entry Margin="0,20,0,0" Placeholder="Enter sample text" Text="{Binding EntryBoxText}"/> <Button Margin="0,20,0,0" Text="Update Text" Command="{Binding UpdateCommand}" /> <Label Margin="0,20,0,0" TextColor="Black" Text="{Binding UpdatedText}"/> </StackLayout>

    MainPage.xaml.cs

    public MainPage() { InitializeComponent(); this.BindingContext = new MainPageViewModel(Navigation); }

    MainPageViewModel.cs

    ``` private string _updatedText = "Updated text appear here."; public string UpdatedText { get {

                return _updatedText;
            }
            set
            {
                SetProperty(ref _updatedText, value);
            }
        }
        private string _entryBoxText;
        public string EntryBoxText
        {
            get
            {
    
                return _entryBoxText;
            }
            set
            {
                SetProperty(ref _entryBoxText, value);
            }
        }
        public INavigation Navigation { get; set; }
        public ICommand UpdateCommand { private set; get; }
        public MainPageViewModel(INavigation navigation)
        {
            this.Navigation = navigation;
            UpdateCommand = new Command(() =>
            {
                UpdatedText = EntryBoxText;
            });
    
        }
    

    ```

    NotifyPropertyBase.cs

    ``` public abstract class NotifyPropertyBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual bool SetProperty(ref T field, T value, [CallerMemberName] string propertyName = null) { var result = false;

            if (field != null && !field.Equals(value))
            {
                field = value;
                result = true;
                OnPropertyChanged(propertyName);
            }
            else if (field == null && value != null)
            {
                field = value;
                result = true;
                OnPropertyChanged(propertyName);
            }
    
            return result;
        }
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, September 17, 2020 11:00 AM