Here's a link of a 'Ticker' class that uses INotifyPropertyChanged so it'll auto-update. Here's the code from the site:
namespace TheJoyOfCode.WpfExample
{
public class Ticker : INotifyPropertyChanged
{
public Ticker()
{
Timer timer = new Timer();
timer.Interval = 1000; // 1 second updates
timer.Elapsed += timer_Elapsed;
timer.Start();
}
public DateTime Now
{
get { return DateTime.Now; }
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Now"));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
<Page.Resources>
<src:Ticker x:Key="ticker" />
</Page.Resources>
<TextBox Text="{Binding Source={StaticResource ticker}, Path=Now, Mode=OneWay}"/>
Declare:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Now this will work:
<TextBox Text="{Binding Source={StaticResource ticker}, Path=Now, Mode=OneWay}"/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…