Title: Label Content binding only updating once
Tags: c#;wpf;mvvm
Question: My label only seems to get the data from the property it is bound to once. I have the Property raising the Property Changed event in the setter, but when the value of the property gets changed, it raises the event properly (I know this because of the break point I set), but the text in the Label on the window doesn't change. I should maybe also note that the window with the label isn't the main window, but a new one that pops up.
ViewModel:
public class PurchaseVerificationViewModel : ViewModelBase
private WindowService.WindowService windowService = new WindowService.WindowService();
private string _verificationQuestion = "Question"; //default so i can check if it changed in the window
public string VerificationQuestion
get { return _verificationQuestion; }
set
{
if (_verificationQuestion != value)
{
_verificationQuestion = value;
OnPropertyChanged(nameof(VerificationQuestion));
}
}
Window:
<Window&gt;
<Window.DataContext&gt;
<viewmodels:PurchaseVerificationViewModel/&gt;
</Window.DataContext&gt;
<Grid&gt;
<Label Content="{Binding VerificationQuestion, UpdateSourceTrigger=PropertyChanged}"/&gt;
</Grid&gt;
</Window&gt;
ViewModelBase:
public class ViewModelBase : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

Here is the accepted answer: There's no problem with your implementation of the INotifyPropertyChanged, since you are correctly invoking the notification that your property has been modified.
So if the problem is not with the one who's raising the notification, might it rather be with what is actively listening to it?
And the problem is that you're defining the DataContext of your Window to the class itself, rather than to the instance which you are utilizing and modifying in the code-behind of your application.
What is actually happening under the hoods, due to the way you defined your DataContext in xaml, is that a new PurchaseVerificationViewModel class is being constructed (is the not the one who are modifying on your logic) and therefore your VerificationQuestion will return it's default value (or rather the private backing field default value, "Question").
In reality the problem is that you have induced your listener to listen to the wrong thing.
Since you want the content of the Label (target) to be update based on a source change, what you have to do, is to set as the DataContextof the Window the specific instance which you are modifying on the logic of your application, and make sure you define it as a property!
public PurchaseVerificationViewModel myViewModel {get;set;}
For instance after InitializeComponent(), on your page constructor, you could initialize the property and set it as the DataContext, like this:
myViewModel = new PurchaseVerificationViewModel();
this.DataContext = myViewModel;
Comment for this answer: This definitely led me on the right track. Had to do quite a bit of modifications specific to my project, but this is exactly what was going on.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:02/15/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><wpf><mvvm>', 'Title': 'Label Content binding only updating once', 'ViewCount': 243, 'id': 48816545, 'user_AboutMe': None, 'user_AccountId': 8883314.0, 'user_DisplayName': 'Jorg1776', 'user_Reputation': 25.0, 'user_Views': 21.0, 'user_id': 6632519.0}}