locked
How do I set “OnPropertyChanged” on Properties from Model? RRS feed

  • Question

  • I Have Two ComboBoxes for example Country and State where State ComboBox's ItemSource depends on Selected Country of First ComboBox. Country and State Properties are defined in separate CountryModel(CountryID,CountryName) and StateModel(CountryID,StateID,StateName). Now there is the Model "UserModel" is like :

     public class UserModel:ViewModelBase
    {
        private string _userName;
    
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; OnPropertyChanged("UserName"); }
        }
    
    
        private long _stateID;
    
        public long StateID
        {
            get { return _stateID; }
            set { _stateID = value; OnPropertyChanged("StateID"); }
        }
    
    
        private int _countryID;
    
        public int CountryID
        {
            get { return _countryID; }
            set { _countryID = value; OnPropertyChanged("CountryID"); }
        }
    }

    It has a Service "UserModelService" for populating :

    public class UserModelService
    {
        public UserModelService()
        {
    
        }
        public ObservableCollection<UserModel> GetUserList()
        {...}
    
        public ObservableCollection<CountryModel> GetCountryList()
        {...}
    
        public ObservableCollection<StateModel> GetStateList(int countryID)
        {...}
    }

    Now The UserViewModel is Like:

    public class UserViewModel:ViewModelBase,IPageViewModel
    {
       
        UserModelService modelService;
        public UserViewModel()
        {
            modelService = new UserModelService();
            GetData();
        }
        private void GetData()
        {
            UserList = modelService.GetUserList();
            CountryList = modelService.GetCountryList();
            StateList = modelService.GetStateList();
            
        }
        private UserModel _currentUser=new UserModel();
    
        public UserModel CurrentUser
        {
            get { return _currentUser; }
            set
            {
                if (value == _currentUser) return;
                _currentUser = value; OnPropertyChanged("CurrentUser");
            }
        }
    
        private ObservableCollection<UserModel> _userList;
        public ObservableCollection<UserModel> UserList
        {
            get { return _userList; }
            set
            {
                if (value == _userList) return;
                _userList = value;
                OnPropertyChanged("UserList");
            }
        }
        private ObservableCollection<CountryModel> _countryList;
    
        public ObservableCollection<CountryModel> CountryList
        {
            get { return _countryList; }
            set { _countryList = value; OnPropertyChanged("CountryList"); }
        }
        private ObservableCollection<StateModel> _stateList;
        public ObservableCollection<StateModel> StateList
        {
            get { return _stateList; }
            set
            {
                _stateList = value;
                OnPropertyChanged("StateList");
            }
        }
    
    }

    Now the Country and State ComboBox's ItemSource is binded to UserViewModels "CountryList" and "StateList" respectively. In this scenario how to I repopulate StateList OnPropertyChange of CurrentUser.CountryID?

    I tried Property Change Event for currentUser like:

    public UserModel CurrentUser
    {
        get { return _currentUser; }
        set
        {
            if (value == _currentUser)
                return;
            if (_currentUser != null)
                _currentUser.PropertyChanged -= OnCurrentUserPropertyChanged;
            _currentUser = value;
            if (_currentUser != null)
                _currentUser.PropertyChanged += OnCurrentUserPropertyChanged;
            OnPropertyChanged("CurrentUser");
        }
    }
    
    private void OnCurrentUserPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(UserModel.CountryID))
        {
            UserModel userModel = (UserModel)sender;
            
            StateList = modelService.GetStateList(UserModel.CountryID);
        }
    }

    But UserModel.CountryID PropertyChange is not even Firing.


    Wednesday, November 25, 2020 6:44 AM

All replies

  • Hello,

    See the following code sample. Other than using the older form of INotifyPropertyChanged seems this would be helpful.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    Wednesday, November 25, 2020 11:42 AM