在标签/文本块中显示并刷新当前日期和时间

问题描述:

是否可以在WPF标签/文本块中显示和刷新当前日期和时间? 也许一些示例代码?在标签/文本块中显示并刷新当前日期和时间

+1

是的,这是可能的。但你到目前为止尝试过什么? – Zabavsky

+1

参考这[链接](http://stackoverflow.com/questions/9490333/updating-a-datetime-to-a-label-in-wpf)..这就是你要找的可能 – Glk

+0

**是** ,这是可能的:P – atiyar

是的,这是可能的!我的解决方案(在自定义WPF组件中)非常类似于this solution,仅与DependencyProperty,XAML中的绑定和时间/日期格式相关。

CS:

//... 
void timer_Tick(object sender, EventArgs e) 
{ 
    Time = DateTime.Now; 
} 

public DateTime Time 
{ 
    get { return (DateTime)GetValue(TimeProperty); } 
    set { SetValue(TimeProperty, value); } 
} 

public static readonly DependencyProperty TimeProperty = DependencyProperty.Register(nameof(Time), typeof(DateTime), typeof(ChannelOverview), new PropertyMetadata(DateTime.Now)); 

XAML:

<TextBox Text="{Binding Time, StringFormat='{}{0:HH:mm}'}"/>