'WPF Binding not updating even though PropertyChanged fires
I have a view model that I bind to my view via
public MyViewModel viewModel = new MyViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = viewModel;
}
MyViewModel implements the INotifyPropertyChanged interface with the standard
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
MyViewModel has a string member called Status which is defined with
private string _status = string.Empty;
public string Status
{
get => _status;
set
{
if (_status != value)
{
_status = value;
OnPropertyChanged();
}
}
}
The Status property is used in my view like so:
<TextBlock Text="{Binding Status}"></TextBlock>
As my application runs, the value of Status periodically changes. I can see that OnPropertyChanged fires correctly in each case, however the view only changes some of the time. I have also noticed (by logging to the console when a property changes) that the property sometimes changes (and fires the change event) but then takes a few seconds before the View updates randomly.
I believe this has something to do with the binding. I can't afford to manually trigger a bindingExpression.UpdateTarget() manually, as the property changes when certain things happen under the hood of the view model (outside of the influence of the view).
I expected the view to update as soon as the binded property changed, but it did not.
I have tried changing the binding mode to TwoWay as well as binding the View Model within the XAML but nothing changes. There must be something I am missing. Appreciate any help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
