Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf,How to bind Current Date?

Tags:

wpf

I have a TextBlock control to which I would like to bind current System Date , how can I do that by Code Behind ?

The goal is to display in this TecBlock the current System Date and Time and I do not need the control refresh all the time ,only once.

I hope that is most simple Code.I don't want to Create dateTime Property. follow is my code:it is Wrong that it can't find BindSource

  Binding bd = new Binding("System.DateTime.Now");
        bd.Source = this;
        textBox.SetBinding(TextBox.TextProperty, bd);

Thanks for help

like image 806
doull Avatar asked Nov 28 '22 15:11

doull


1 Answers

This will show the Current date only once .

create a namespace alias:

  xmlns:sys="clr-namespace:System;assembly=mscorlib"


<TextBlock Text="{Binding Source={x:Static sys:DateTime.Today},   
       StringFormat='{}{0:dddd, MMMM dd, yyyy}'}"/> 
like image 66
Kishore Kumar Avatar answered Dec 05 '22 16:12

Kishore Kumar