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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespaceWpfApplication9
{
publicpartialclassTimer
{
private Timer clockTimer;
publicTimer();
{
clockTimer = new Timer(1000);
clockTimer.Elapsed += new ElapsedEventHandler(clockTimer_Elapsed);
clockTimer.Start();
DataContext = this;
}
voidclockTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(UpdateCurrentDateTime));
}
publicstring CurrentDateTime
{
get { return (string)GetValue(CurrentDateTimeProperty); }
set { SetValue(CurrentDateTimeProperty, value); }
}
publicstaticreadonly DependencyProperty CurrentDateTimeProperty =
DependencyProperty.Register("CurrentDateTime", typeof(string), typeof(Mainwindow), new UIPropertyMetadata(string.Empty));
privatevoidUpdateCurrentDateTime()
{
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
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 2, 2010, 8:00 AM EST
0
I am not familiar with WPF so I cannot help much, but if this were a Forms application, then I would create a UserControl, but UserControls in WPF appear to be complicated so I don't know how to do it as a UserControl in WPF. Apparently in WPF you can derive from either UserControl or Control, both with advantages relative to the other.
I think you should create either a UserControl or a Control that has a Timer and a TextBox in it. You can create the Timer in code if you want to or drag and drop from the toolbox. You will need to use Invoke or something similar to update the TextBox. UserControls and Controls are a very effective way to modularize your code.
Raaj KumarRank #7,836 · 233 reputationPosted Mar 3, 2010, 12:57 AM EST
0
Hi,
Nothing big you have to do simply inherit INotifyPropertyChanged and implement the event. Like this,
public string CurrentDateTime { get { return (string)GetValue(CurrentDateTimeProperty); } set { SetValue(CurrentDateTimeProperty, value); PropertyChanged(this, new PropertyChangedEventArgs("CurrentDateTime")); } }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = (t, e) => { };
Sam HobbsRank #107 · 20.5K reputationPosted Mar 2, 2010, 8:00 AM EST
I think you should create either a UserControl or a Control that has a Timer and a TextBox in it. You can create the Timer in code if you want to or drag and drop from the toolbox. You will need to use Invoke or something similar to update the TextBox. UserControls and Controls are a very effective way to modularize your code.
Raaj KumarRank #7,836 · 233 reputationPosted Mar 3, 2010, 12:57 AM EST
Nothing big you have to do simply inherit INotifyPropertyChanged and implement the event. Like this,
public string CurrentDateTime
{
get { return (string)GetValue(CurrentDateTimeProperty); }
set
{
SetValue(CurrentDateTimeProperty, value);
PropertyChanged(this, new PropertyChangedEventArgs("CurrentDateTime"));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged = (t, e) => { };
#endregion
Try this and let me know if you face any problem
Regards,
raaj
RIck Mueller45 reputationPosted Mar 11, 2010, 10:57 AM EST
Thank you for your help, Still not able to get it wired up