C#C
C#2y ago
kianyuen

ViewModel Command ran when I debugged, but the View is not updated

This is my ViewModel:
   public class MainViewModel : ViewModelBase
    {
        private ViewModelBase _currentChildView;
        private string _title;

        public ViewModelBase CurrentChildView
        {
            get
            {
                return _currentChildView;
            }
            set
            {
                _currentChildView = value;
                OnPropertyChanged(nameof(CurrentChildView));
            }
        }
        public string Title
        {
            get => _title;
            set
            {
                _title = value;
                OnPropertyChanged(nameof(Title));
            }
        }

        // Commands for user interactions
        public ICommand ShowBloodPressureViewCommand { get; }
        public ICommand ShowBMIViewCommand { get; }
        public ICommand ShowHeightWeightViewCommand { get; }


        public MainViewModel()
        {
            ShowBloodPressureViewCommand = new RelayCommand(ExecuteShowBloodPressureViewCommand);
            ShowBMIViewCommand = new RelayCommand(ExecuteShowBMIViewCommand);
            ShowHeightWeightViewCommand = new RelayCommand(ExecuteShowHeightWeightViewCommand);

            ExecuteShowBloodPressureViewCommand(null);
        }

        private void ExecuteShowHeightWeightViewCommand(object obj)
        {
            CurrentChildView = new HeightWeightViewModel();
            Title = "Height Weight";

        }

        private void ExecuteShowBMIViewCommand(object obj)
        {
            CurrentChildView = new BMIViewModel();
            Title = "BMI";
        }

        private void ExecuteShowBloodPressureViewCommand(object obj)
        {
            CurrentChildView = new BloodPressureViewModel();
            Title = "Blood Pressure";
        }
    }


When initialized, ExecuteShowBloodPressureViewCommand ran normally, but when I use UI to run other commands, the Execute function still ran, but the View doesn't update.
Was this page helpful?