Besides using a BackgroundWorker, that is…
I have a text box and a button. When I press the button, the text box is supposed to display 0, then count up once per second from 0 to 10. I can establish a bind between a string property and the text box’s text, but when I press the button, it waits 10 seconds, then displays “10” in the box.
Here is the XAML:
<Window x:Class="Generic_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Generic_WPF"
mc:Ignorable="d"
x:Name="_this"
Title="MainWindow" Height="1024" Width="768">
<TextBox x:Name="Text_Output"
Margin="20,80,20,20"
TextWrapping="Wrap"
DataContext="{Binding ElementName=_this}"
Text="{Binding TheText}" />
</Window>
and here is the C# code:
public partial class MainWindow : Window
{
public static readonly DependencyProperty TheTextProperty = DependencyProperty.Register("TheText", typeof(string), typeof(MainWindow));
public string TheText
{
get { return (string)GetValue(TheTextProperty); }
set { SetValue(TheTextProperty, value); }
}
private int TheCount = 0;
private void Button_Run_Click(object sender, RoutedEventArgs e)
{
TheCount = 0;
while (TheCount <= 10)
{
TheText = TheCount.ToString();
DateTime StartTime = DateTime.Now;
while ((DateTime.Now - StartTime).TotalSeconds < 1.0) ;
TheCount++;
}
}
}
Does there need to be something in Button_Run_Click to signal that the GUI needs to be refreshed (similar to (control).Refresh() and Application.DoEvents() in Windows Forms)? Or does something need to be added on the XAML side?