title: Time Ticking in C# WPF MVVM
body: <p>I am doing a program, which must have a clock hanging every second, main problem is that I am beginner in WPF and MVVM :)</p>
  
  <p>But otherwise my clock is running just not refreshing. I have special class for only Time and Date purpose.</p>
  
  <p>Here is my code:</p>
  
  <p>TimeDate class:</p>
  
  <pre><code>public class TimeDate : ViewModelBase
  {
  
      public string SystemTimeHours;
      string SystemTimeLong;
      string SystemDateLong;
      string SystemTimeZoneLong;
  
      string SystemTimeShort;
      string SystemDateShort;
      string SystemTimeZoneShort;
  
  
  
      public void InitializeTimer()
      {
  
          DispatcherTimer timer = new DispatcherTimer();
          timer.Interval = TimeSpan.FromSeconds(1);
          timer.Tick += new EventHandler(timer_Tick);
          timer.Start();
      }
  
      void timer_Tick(object sender, EventArgs e)
      {
  
          SystemTimeHours = DateTime.Now.ToString("HH:mm tt");
  
      }
  }
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>public class ViewModel : ViewModelBase
  {
      public ViewModel()
      {
          TimeDate td = new TimeDate();
          td.InitializeTimer();
          HoursTextBox = td.SystemTimeHours;
  
      }
  
  
  
      private string _hourstextbox;
      public string HoursTextBox
      {
          get
          { return _hourstextbox; }
          set
          {
              if (value != _hourstextbox)
              {
                  _hourstextbox = value;
                  NotifyPropertyChanged("HoursTextBox");
              }
          }
      }
  }
  </code></pre>
  
  <p>And also NotifyProperty in ViewModelBase:</p>
  
  <pre><code>public class ViewModelBase : INotifyPropertyChanged, IDisposable
  {
      #region Constructor
  
      protected ViewModelBase()
      {
      }
  
      #endregion // Constructor
  
      #region DisplayName
  
  
      public virtual string DisplayName { get; protected set; }
  
      #endregion // DisplayName
  
      #region Debugging Aides
  
      [Conditional("DEBUG")]
      [DebuggerStepThrough]
      public void VerifyPropertyName(string propertyName)
      {
          // Verify that the property name matches a real,  
          // public, instance property on this object.
          if (TypeDescriptor.GetProperties(this)[propertyName] == null)
          {
              string msg = "Invalid property name: " + propertyName;
  
              if (this.ThrowOnInvalidPropertyName)
                  throw new Exception(msg);
              else
                  Debug.Fail(msg);
          }
      }
  
  
      protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
  
      #endregion // Debugging Aides
  
      #region INotifyPropertyChanged Members
  
  
      public event PropertyChangedEventHandler PropertyChanged;
  
  
      /// &lt;param name="propertyName"&gt;The property that has a new value.&lt;/param&gt;
      protected virtual void NotifyPropertyChanged(string propertyName)
      {
          this.VerifyPropertyName(propertyName);
  
          PropertyChangedEventHandler handler = this.PropertyChanged;
          if (handler != null)
          {
              var e = new PropertyChangedEventArgs(propertyName);
              handler(this, e);
          }
      }
  
      protected virtual void NotifyPropertyChangedAll(object inOjbect)
      {
          foreach (PropertyInfo pi in inOjbect.GetType().GetProperties())
          {
              NotifyPropertyChanged(pi.Name);
          }
      }
      public virtual void Refresh()
      {
          NotifyPropertyChangedAll(this);
      }
      #endregion // INotifyPropertyChanged Members
  
      #region IDisposable Members
  
      public void Dispose()
      {
          this.OnDispose();
      }
  
      /// &lt;summary&gt;
      /// Child classes can override this method to perform 
      /// clean-up logic, such as removing event handlers.
      /// &lt;/summary&gt;
      protected virtual void OnDispose()
      {
      }
  
  
      /// &lt;/summary&gt;
      ~ViewModelBase()
      {
          string msg = string.Format("{0} ({1}) ({2}) Finalized", this.GetType().Name, this.DisplayName, this.GetHashCode());
          System.Diagnostics.Debug.WriteLine(msg);
      }
  
  
      #endregion // IDisposable Members
  
  }
  </code></pre>
  
  <p>What to do, that Clock will refresh every second? Please help</p>