I have a simple WPF textbox that I want to update once when the window is initialized, and then each time a certain event occurs. I have followed the instructions in many StackOverflow answers, in particular this one: WPF: simple TextBox data binding So I came up with the following.
In xaml:
<TextBox x:Name="txtFeedback"
TextWrapping="Wrap"
Text="{Binding Path=FeedbackText, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="True"
AcceptsReturn="True"/>
And in the MainWindow:
private string _feedbackText;
public event PropertyChangedEventHandler PropertyChanged;
public string FeedbackText
{
get
{
return _feedbackText;
}
set
{
_feedbackText = value;
OnPropertyChanged("FeedbackText");
}
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
FeedbackText = "Awaiting start of process...";
}
private void FinishedWorksheet(object sender, EventArgs e)
{
FeedbackText += "Done another worksheet" ;
}
When the constructor of the form is called, the textbox correctly displays "Awaiting start of process...", but when I call the FinishedWorksheet method (which is called by some other event) the textbox is not updated.
Please note that if I put breakpoints inside OnPropertyChanged I can see that it is called, and in the immediate window I can see the Text property actually changing, but somehow the textbox is not updated.
txtFeedback.Text
"Awaiting start of process...Done another worksheet"
Update
The problem was much simpler, and sorry to everyone for not posting one fundamental part of the code: I was doing everything inside the GUI thread! So of course the GUI could not react until I released the thread! Once I made the external call Asynchronous, it all works fine. Thank you all for your suggestions though, I learned a lot, and I hope others did too.