Timer Design in StatusBar
Timer in StatusBar
- we need to show local time in StatusBar. solution: 1. add textblock control 2. binding to a string. 3. we use dispatcherTimer class to update the string.
- DispatcherTimer class
- Code Example:
XAML:
<StatusBarItem HorizontalAlignment="Left" Margin="10,0,0,0">
<TextBlock Text="当前时间: " />
</StatusBarItem>
<StatusBarItem HorizontalAlignment="Left">
<TextBlock Text="{Binding CurrentTime}" />
</StatusBarItem>
c#:
public string CurrentTime
{
get
{
return _currentTime;
}
set
{
_currentTime = value;
RaisePropertyChanged("");
}
}
private void UpdateTime( object sender, EventArgs e)
{
CurrentTime = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
}
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0,0,1);
timer.Tick += new EventHandler(UpdateTime);
timer.Start();