I am having a problem with updating the Window of a Wpf-Application. After googling a long time, I broke my problem down to the following example. I hope you can help me.
Let´s say we have a Window with a Button and a TextBlock ... nothing special:
...
<Grid>
<Button ... Click="button_Click"/>
<TextBox x:Name="textBox" Text="DefaultText" .../>
</Grid>
...
The button_Click is a simple event like this:
private void button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 20; i++)
{
textBox.Dispatcher.Invoke(new Action(() => textBox.Text = i.ToString());
}
}
And here is my Problem. If I debug the loop, the Text of my TextBlock is always the "DefaultText". Only at the end of my loop, the Text is "19". Why is my dispatcher not able to change the Text? Like described here (What's the difference between Invoke() and BeginInvoke()) the Dispatcher should stop my Thread until the job is done. I tried different DispatcherPriorities ... but no difference. What´s my problem?
Thank you for helping me.