0

Is it possible to show and refresh the current date and time in a WPF label/textblock? Maybe some sample code?

Draken
  • 3,134
  • 13
  • 34
  • 54
user1617141
  • 115
  • 1
  • 11

1 Answers1

0

Yes, it is possible! My solution (in custom WPF component) is very similar like this solution only with DependencyProperty, binding and time/date formatting in 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}'}"/>
honzakuzel1989
  • 2,400
  • 2
  • 29
  • 32