First look at the effect:
This is actually what I placed two TextBlock, which shows the current date, and shows the time below.
Next, show the code:
In XAML:
<stackpanel width= "205"
Margin= "0,0,57,0"
Horizontalalignment= "Right" >
<textblock height= "Auto"
Margin= "10,5,0,0"
Name= "Tbdatetext"
foreground= "White"
fontweight= "Bold"
Fontfamily= "Arial"
Fontsize= "/>"
<textblock height= "Auto"
Margin= "10,5,0,0"
Name= "Tbtimetext"
Foreground= "#ffa51f"
fontweight= "Bold"
Fontfamily= "Calibri"
Fontsize= "/>"
</StackPanel>
In the main form of CS, the code is:
<summary>
Define a timer
</summary>
Private DispatcherTimer Showtimer;
Public MainWindow ()
{
InitializeComponent ();
ShowTime (); When the form is loaded, the text box is not assigned, and the time is not displayed on the form, but it waits for a moment to show it.
Showtimer = new System.Windows.Threading.DispatcherTimer ();
Showtimer.tick + = new EventHandler (showcurtimer);//A timer always gets the current time
Showtimer.interval = new TimeSpan (0, 0, 0, 1, 0);
Showtimer.start ();
}
public void Showcurtimer (object sender, EventArgs e)
{
ShowTime ();
}
Showtime Method
private void ShowTime ()
{
Acquired date
This.tbDateText.Text = DateTime.Now.ToString ("Yyyy/mm/dd"); Yyyy/mm/dd
Get time and seconds
This.tbTimeText.Text = DateTime.Now.ToString ("HH:mm:ss");
}