public void AddSchedule(ScheduledTime oTime) { //int iDay = (DateTime.Now.Date - oTime.StartDateTime).Days; int iDay = (oTime.StartDateTime.Date - DateTime.Now.Date).Days; //Check if startTime is in the past if (oTime.StartDateTime.Date < DateTime.Now.Date) { //Show only the hours that are visible today oTime.Duration = (oTime.StartDateTime + oTime.Duration) - DateTime.Now.Date; oTime.StartDateTime = DateTime.Now.Date; iDay = 0; } if (iDay < DaysVisible & iDay >= 0) { DayControl DC = (DayControl)stackPanel2.Children[iDay]; TimeSpan startTime = oTime.StartDateTime.TimeOfDay; TimeSpan endTime = oTime.StartDateTime.TimeOfDay + oTime.Duration; if (endTime.TotalHours > 24) { //Move it to next day endTime = new TimeSpan(24, 0, 0); AddSchedule(new ScheduledTime(new DateTime(oTime.StartDateTime.Year, oTime.StartDateTime.Month, oTime.StartDateTime.Day, 0, 0, 0).AddDays(1), (oTime.StartDateTime.TimeOfDay + oTime.Duration).Subtract(new TimeSpan(24, 0, 0)), oTime.color, oTime.Title, oTime.isLocal, oTime.ServiceWindowID, oTime.ServiceWindowType));; } if ((oTime.StartDateTime.Date - DateTime.Now.Date) <= new TimeSpan(DaysVisible, 0, 0, 0)) { DC.timeList.Add(new DayControl.timeRange(startTime, endTime, oTime.color, oTime.Title, oTime.isLocal, oTime.ServiceWindowID, oTime.ServiceWindowType)); } } }
private void UserControl_Loaded(object sender, RoutedEventArgs e) { stackPanel2.Children.Clear(); for (int iDay = 1; iDay <= DaysVisible; iDay++) { DayControl DC = new DayControl(); DC.Width = DaysDock1.ActualWidth / DaysVisible; DC.Name = "Day" + iDay.ToString(); DC.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; DC.DeleteClick += DC_DeleteClick; List <DayControl.timeRange> tList = new List <DayControl.timeRange>(); stackPanel2.Children.Add(DC); Label lDay = new Label(); lDay.Content = Startdate.AddDays(iDay - 1).ToString("dddd", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat); lDay.Width = DaysDock1.ActualWidth / DaysVisible; lDay.Margin = new Thickness(0, -2, 0, 0); HeaderStack1.Children.Add(lDay); } List <ScheduledTime> NextTimes = new List <ScheduledTime>(); foreach (ScheduledTime oTime in ScheduledTimes) { AddSchedule(oTime); } }