I am trying to get a textblock to display the running time of my stopwatch but am unsure how I would accomplish this.
I've tried using a binding to the stopwatch's elapsed method but this doesn't display anything.
<TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,0,398,0" Name="textBlockElapsed" Text="{Binding Path=watch.Elapsed, Mode=OneWay}" VerticalAlignment="Top" />
Stopwatch watch = new Stopwatch();
Try the following example code: MainWindow.xaml.cs
public partial class MainWindow : Window
{
DispatcherTimer dt = new DispatcherTimer();
Stopwatch stopWatch = new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (Environment.HasShutdownStarted)
{
MessageBox.Show("Total amount of time the system logged on:" + ClockTextBlock.Text);
}
}
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
switch (e.Reason)
{
// ...
case SessionSwitchReason.SessionLock:
if (stopWatch.IsRunning)
stopWatch.Stop();
break;
case SessionSwitchReason.SessionUnlock:
stopWatch.Start();
dt.Start();
break;
case SessionSwitchReason.SessionLogoff:
MessageBox.Show("Total amount of time the system logged on:" + ClockTextBlock.Text);
break;
// ...
}
}
void dt_Tick(object sender, EventArgs e)
{
if (stopWatch.IsRunning)
{
TimeSpan ts = stopWatch.Elapsed;
currentTime = String.Format("{0:00}:{1:00}:{2:00}",
ts.Hours, ts.Minutes, ts.Seconds);
ClockTextBlock.Text = currentTime;
}
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
stopWatch.Start();
dt.Start();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
if (stopWatch.IsRunning)
stopWatch.Stop();
}
private void ResetButton_Click(object sender, RoutedEventArgs e)
{
stopWatch.Reset();
stopWatch.Start();
}
}
MainWindow.xaml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.3*"/>
<RowDefinition Height="0.2*"/>
</Grid.RowDefinitions>
<TextBlock Name="ClockTextBlock"
TextAlignment="Center"
VerticalAlignment="Center"
FontSize="35" Foreground="Red"
Grid.ColumnSpan="4"
Grid.Row="0" />
<Button Content="Start"
Name="StartButton"
Grid.Row="1"
Grid.Column="0"
Width="60" Height="35"
Click="StartButton_Click" />
<Button Content="Stop"
Name="StopButton"
Grid.Row="1"
Grid.Column="1"
Width="60" Height="35"
Click="StopButton_Click" />
<Button Content="Reset"
Name="ResetButton"
Grid.Row="1"
Grid.Column="3"
Width="60" Height="35"
Click="ResetButton_Click" />
</Grid>
Stopwatch doesn't have an Elapsed method. It has an Elapsed property, but that's only updated when you query it.
Lolo's answer shows how to bind the Elapsed property to your text field. If you want the display to continually update, you'll need to create a timer that will trigger an update of that field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With