'Observe variable in ViewModel from View in WPF
I have a boolean variable in my ViewModel and I want to observe any changes to it. ViewModel:
internal class AuthenticationViewModel : ViewModelBase
{
APIClient apiClient;
bool _loginStatus;
public AuthenticationViewModel()
{
}
public bool LoginStatus { get { return _loginStatus; }
set {
_loginStatus = value;
NotifyPropertyChanged("LoginStatus");
} }
}
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I am trying to use it in my View as following:
public MainWindow()
{
InitializeComponent();
ViewModel = new AuthenticationViewModel();
_ = ViewModel.GetReadyForUnlockWithBLEAsync();
if(ViewModel.LoginStatus)
{
AuthenticationSuccess();
}
}
But I am not able to observe the variable from ViewModel. I can't get its updated value in View on any changes in ViewModel.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
