-1

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.

dotnetengineer
  • 1,254
  • 1
  • 9
  • 18
Ryan
  • 97
  • 1
  • 2
  • 7

1 Answers1

3

Your Binding statement for the Content has a syntax error, and it is wrong. should be:

Content="{Binding Now}"

or (identical):

Content="{Binding Path=Now}"

No need for 'Source' (and if you have multiple properties in Binding - you must have a comma).

For WPF resources, search in this site for [wpf] - the question with the most votes is a summary of resources: MVVM: Tutorial from start to finish?

Community
  • 1
  • 1
Uri London
  • 10,631
  • 5
  • 51
  • 81
  • Hey thanks Uri! It does display now, although it is not updating... Is 'Source' not necessary because I've set the labels datacontext to the time? Thanks again. – Ryan Feb 29 '12 at 20:38
  • No need for 'Source'. You are setting it with code, when you are setting lastUpdate.DataContext. I can't see anything that will prevent your code to update the UI. Please double check your syntax. Very often people makes Typo in the name of the property when they fire an event (the compiler doesn't verify the property exist). Step with the debugger from the Update function, through the setter, and verify an event is fired. Beyond that, I don't know what to do. – Uri London Feb 29 '12 at 20:59