Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

Editable TimeSpan

4.60/5 (3 votes)
16 Nov 2014CPOL 9.2K  
A TimeSpan wrapper class which uses a System.TimeSpan as a backing field.

Introduction

In WPF, you can use bindings to bind data properties with controls. However, instances of TimeSpan structure are immutable, so you cannot create a two-way binding to its Hours, Minutes, Seconds, etc. properties. The code below is a solution for this problem.

Using the Code

Here it is:

C#
public class EditableTimeSpan : INotifyPropertyChanged
 {
     TimeSpan _timespan;

     public TimeSpan TimeSpan
     {
         get { return _timespan; }
         set { _timespan = value; FireAll(); }
     }
     public int Hours
     {
         get { return TimeSpan.Hours; }
         set
         {
             if (TimeSpan.Hours != value)
             {
                 TimeSpan = new TimeSpan(value, TimeSpan.Minutes, TimeSpan.Seconds);
                 FireAll();
             }
         }
     }
     public int Minutes
     {
         get { return TimeSpan.Minutes; }
         set
         {
             if (TimeSpan.Minutes != value)
             {
                 TimeSpan = new TimeSpan(TimeSpan.Hours, value, TimeSpan.Seconds);
                 FireAll();
             }
         }
     }
     public int Seconds
     {
         get { return TimeSpan.Seconds; }
         set
         {
             if (TimeSpan.Seconds != value)
             {
                 TimeSpan = new TimeSpan(TimeSpan.Hours, TimeSpan.Minutes, value);
                 FireAll();
             }
         }
     }
     public double TotalSeconds
     {
         get { return TimeSpan.TotalSeconds; }
         set
         {
             TimeSpan = TimeSpan.FromSeconds(value);
             FireAll();
         }
     }
     public static explicit operator TimeSpan(EditableTimeSpan ets)
     {
         return ets.TimeSpan;
     }
     void FireAll()
     {
         if (PropertyChanged != null)
         {
             PropertyChanged(this, MinutesArgs);
             PropertyChanged(this, HoursArgs);
             PropertyChanged(this, TotalSecondsArgs);
             PropertyChanged(this, SecondsArgs);
         }
     }
     public static explicit operator EditableTimeSpan(TimeSpan ets)
     {
         return new EditableTimeSpan(ets);
     }
     public event PropertyChangedEventHandler PropertyChanged;
     private PropertyChangedEventArgs MinutesArgs = new PropertyChangedEventArgs("Minutes");
     private PropertyChangedEventArgs TotalSecondsArgs = new PropertyChangedEventArgs("TotalSeconds");
     private PropertyChangedEventArgs SecondsArgs = new PropertyChangedEventArgs("Seconds");
     private PropertyChangedEventArgs HoursArgs = new PropertyChangedEventArgs("Hours");

     public override string ToString()
     {
         return TimeSpan.ToString();
     }
     public string ToString(string format)
     {
         return TimeSpan.ToString(format);
     }
     public EditableTimeSpan()
     {
         _timespan = new TimeSpan();
     }
     public EditableTimeSpan(TimeSpan timeSpan)
     {
         _timespan = timeSpan;
     }
 }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)