我想将文本框内容设置为当前日期和时间。所以这是:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
date_TextBox.Text = DateTime.Now.ToString(CultureInfo.InvariantCulture);
}
}
但是如何每秒刷新一次呢?请帮忙!
您可以使用DispatcherTimer每秒触发一个事件以更新文本框
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += (s,e) => date_TextBox.Text = DateTime.Now.ToString(CultureInfo.InvariantCulture);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();