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

  • Thread starter Thread starter mapuna
  • Start date Start date
M

mapuna

Guest
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.

Continue reading...
 
Back
Top