3
Answers

Adding a Timer Class to a project.

RIck Mueller

RIck Mueller

14y
3.6k
1

I added a new class.cs to my project. Very simple for some.
I have textbox that I want to populate the date and time. The code is complete works fine. I want to stay organize, I don't really want to add anymore code to the MainWindow.cs. I'm having trouble fomatting the Timer.cs to bind with textbox on the MainWindow.
 
The XAML code is
 
<
TextBox Height="41" Text="{Binding CurrentDateTime}"

 
 
The  Timer.cs is the following:
 
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
WpfApplication9
{
public partial class Timer
{
private Timer clockTimer;
public Timer();
{

clockTimer =
new Timer(1000);
clockTimer.Elapsed +=
new ElapsedEventHandler(clockTimer_Elapsed);
clockTimer.Start();
DataContext =
this;
}
void clockTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Dispatcher.BeginInvoke(
new Action(UpdateCurrentDateTime));
}
 
 
public string CurrentDateTime
{
get { return (string)GetValue(CurrentDateTimeProperty); }
set { SetValue(CurrentDateTimeProperty, value); }
}
public static readonly DependencyProperty CurrentDateTimeProperty =
DependencyProperty.Register(
"CurrentDateTime", typeof(string), typeof(Mainwindow), new UIPropertyMetadata(string.Empty));
private void UpdateCurrentDateTime()
{
CurrentDateTime = DateTime.Now.ToString(
"MM/dd/yyyy hh:mm:ss");
}
}
 

 
This is my first time I'm trying to establish a class(.cs) in a project. any help would be great
 
Thanks

Answers (3)