65.9K
CodeProject is changing. Read more.
Home

Editable TimeSpan

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (3 votes)

Nov 16, 2014

CPOL
viewsIcon

9300

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:

   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;
        }
    }