1

I want to display current time on my screen with continuous updating in WPF screen using MVVM pattern.

I am writing this code in my view model

// creating a property
private string _currentDateTime;
public string CurrentDateTime
{
    get
    {
        return _currentDateTime;
    }
    set
    {
        if (value != _currentDateTime)
        {
            _currentDateTime = value;
            this.RaisePropertyChanged(() => this.CurrentDateTime);
        }
    }
}

and I wrote this method

 public string GetCurrentDateTime()
 {
     try
     {
         DispatcherTimer timer = new DispatcherTimer(new TimeSpan(0, 0, 1), 
             DispatcherPriority.Normal, 
             delegate
             {
                 this.CurrentDateTime = DateTime.Now.ToString("HH:mm:ss");
             },
             this.Dispatcher);

             return CurrentDateTime;
     }
     catch
     {
         return CurrentDateTime;
     }
}

I binded my text block with property but it is showing exception as this.CurrentDateTime is null.

Any suggestion why?

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Kanvas
  • 165
  • 2
  • 7
  • 23

2 Answers2

2

I'm not sure what your intention is with RaisePropertyChanged(() => this.CurrentDateTime).

If it is to take care of MVVM property changed notifications, then this code should be in your VM

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

then your set should be

set
{
    if (value != _currentDateTime)
    {
        _currentDateTime = value;
        OnPropertyChanged("CurrentDateTime");
    }
}

to continually update your time, use a Timer

You can then set the interval to say 1 second and on each timer elapsed event set your CurrentDateTime

CurrentDateTime = DateTime.Now.ToString();
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Anton
  • 7,709
  • 5
  • 31
  • 33
  • 3
    My guess is that RaisePropertyChanged has signature RaisePropertyChanged(Expression> propertyExpression), and that the expression tree is being parsed to resolve the right hand side of the expression into the string property name. I do the same thing with my view models to force early binding between properties and GUI strings, thus eliminating the need to duplicate property name info as properties and also as magic strings. – Erik Dietrich Dec 06 '11 at 07:54
0

I am not sure why this problem is occurring but I achieved the same functionality with this but slight change of code.

I changed the code in GetCurrentDateTime method's try block

try
{
    DispatcherTimer dispatcherTimer = new  System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    dispatcherTimer.Start();

    return CurrentDateTime;
}

and with this I have added a new method for timer

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    // Updating the Label which displays the current second
    this.CurrentDateTime = DateTime.Now.ToString(" HH:mm tt");

    // Forcing the CommandManager to raise the RequerySuggested event
    CommandManager.InvalidateRequerySuggested();
}

Now it is working

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Kanvas
  • 165
  • 2
  • 7
  • 23