I am fairly new to WPF so am not really sure what I am doing wrong here. I want to the current time (Datetime.Now) to be displayed and updated to a label on a window. The time would be called to update in a method that loads data from a database (like a "last updated" idea). This methoud is called every 2 minutes (kept by a timer on a thread) once the user is logged in. I have the following class for the time object...
public class UpdatingTime : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private DateTime _now;
public UpdatingTime()
{
_now = DateTime.Now;
}
public DateTime Now
{
get { return _now; }
private set
{
_now = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Now"));
}
}
}
public void Update()
{
Now = DateTime.Now;
}
}
In the window's class constructor, I have...
UpdatingTime updateTime = new UpdatingTime();
lastUpdate.DataContext = updateTime;
In the method that loads the data from the database, I call the Update() method by...
updateTime.Update();
I think my data binding is the problem though (like I said, I'm pretty new). My label in the xaml file looks like...
<Label Name="lastUpdate" Margin="10" Height="auto" Content="{Binding Source updateTime, Path=Now}"
Visibility="Hidden" FontSize="20" />
The reason that visibility is hidden is because I set it to Visible once the user is logged in, I've tested that and I'm sure its not the issue. To be clear, the backend code is in the code behind file to the corresponding xaml file (eg Window.xaml, Window.xaml.cs), so I do not think I am missing a reference either.
The problem is that when I run the application, nothing at all displays (it compiles, and no exceptions are thrown). I am not sure what I am doing wrong, if anyone could shed some light on this I would greatly appreciate.
Also, if you could mention some good resources to learning and becoming familiar with WPF that you found helpful, that would be awesome. I am not sure what the DataContext really is and if I am using it correctly also.
Thanks.