/// <summary> /// Event handler on thumb is dragged /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnDragDelta(object sender, DragDeltaEventArgs e) { if (sender == _thumbMove) { PlannerTimelinePanel timelinePanel = VisualParent as PlannerTimelinePanel; if (timelinePanel != null) { if (VisualTreeHelper.HitTest(timelinePanel, Mouse.GetPosition(timelinePanel)) == null) { if (_virtualAppointmentItem == null) { _virtualAppointmentItem = new PlannerVirtualAppointmentItem(); _virtualAppointmentItem.DataContext = DataContext; } Planner planner = PlannerHelper.FindVisualParent <Planner>(timelinePanel); planner.PreviewVirtualAppointmentItem(_virtualAppointmentItem); } else { if (_virtualAppointmentItem != null) { Planner planner = PlannerHelper.FindVisualParent <Planner>(timelinePanel); planner.RemoveVirtualAppointmentItem(_virtualAppointmentItem); _virtualAppointmentItem = null; } } } if (_virtualAppointmentItem != null) { _virtualAppointmentItem.Arrange(new Rect(VisualOffset.X + e.HorizontalChange, VisualOffset.Y, ActualWidth, ActualHeight)); } Arrange(new Rect(VisualOffset.X + e.HorizontalChange, VisualOffset.Y, ActualWidth, ActualHeight)); } else if (sender == _thumbLeft) { double width = ActualWidth - e.HorizontalChange; if (width < 10) { width = 10; } Width = width; Arrange(new Rect(VisualOffset.X + e.HorizontalChange, VisualOffset.Y, width, ActualHeight)); } else if (sender == _thumbRight) { double width = ActualWidth + e.HorizontalChange; if (width < 10) { width = 10; } Width = width; Arrange(new Rect(VisualOffset.X, VisualOffset.Y, width, ActualHeight)); } e.Handled = true; }
/// <summary> /// Calculate datetime from given offset from start /// </summary> /// <param name="offset"></param> /// <returns></returns> public DateTime CalculateDateTime(double offset) { DateTime?Start = PlannerHelper.FindVisualParent <Planner>(this).GetValue(Planner.StartProperty) as DateTime?; DateTime?End = PlannerHelper.FindVisualParent <Planner>(this).GetValue(Planner.EndProperty) as DateTime?; TimeSpan timeSpan = End.Value.AddHours(1) - Start.Value; double minutes = offset / (ActualWidth / timeSpan.TotalMinutes); DateTime result = Start.Value.AddMinutes(minutes); return(new DateTime(result.Year, result.Month, result.Day, result.Hour, result.Minute, 0)); }
/// <summary> /// Preview virtual appointment item on a virtual panel /// </summary> /// <param name="appointmentItem"></param> public void PreviewVirtualAppointmentItem(PlannerAppointmentItem appointmentItem) { PlannerEntity targetEntity = PlannerHelper.FindVisualChild <PlannerEntity>(this).Find(_ => VisualTreeHelper.HitTest(_, Mouse.GetPosition(_)) != null); if (targetEntity != null) { if (appointmentItem.Parent != null) { if (appointmentItem.Parent == targetEntity.GetVirtualPanel()) { return; } PlannerTimelinePanel currentPanel = appointmentItem.Parent as PlannerTimelinePanel; currentPanel.Children.Remove(appointmentItem); } PlannerTimelinePanel virtualPanel = targetEntity.GetVirtualPanel(); virtualPanel.Children.Add(appointmentItem); } }
/// <summary> /// Event handler on thumb drag is completed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnDragCompleted(object sender, DragCompletedEventArgs e) { PlannerTimelinePanel timelinePanel = PlannerHelper.FindVisualParent <PlannerTimelinePanel>(this); if (timelinePanel == null) { return; } if (sender == _thumbMove) { TimeSpan timeSpan = End - Start; Start = timelinePanel.CalculateDateTime(VisualOffset.X); End = Start + timeSpan; if (_virtualAppointmentItem != null) { IPlannerEntity currentEntity = PlannerHelper.FindVisualParent <PlannerEntity>(this).DataContext as IPlannerEntity; IPlannerEntity targetEntity = PlannerHelper.FindVisualParent <PlannerEntity>(_virtualAppointmentItem).DataContext as IPlannerEntity; IAppointment appointment = DataContext as IAppointment; currentEntity.Appointments.Remove(appointment); targetEntity.Appointments.Add(appointment); Planner planner = PlannerHelper.FindVisualParent <Planner>(timelinePanel); planner.RemoveVirtualAppointmentItem(_virtualAppointmentItem); _virtualAppointmentItem = null; } } else if (sender == _thumbLeft) { Start = timelinePanel.CalculateDateTime(VisualOffset.X); } else if (sender == _thumbRight) { End = timelinePanel.CalculateDateTime(VisualOffset.X + ActualWidth); } e.Handled = true; }
/// <summary> /// Arrange override /// </summary> /// <param name="finalSize"></param> /// <returns></returns> protected override Size ArrangeOverride(Size finalSize) { double top = 0; double left = 0; double width = 0; double height = 0; DateTime?Start = PlannerHelper.FindVisualParent <Planner>(this).GetValue(Planner.StartProperty) as DateTime?; DateTime?End = PlannerHelper.FindVisualParent <Planner>(this).GetValue(Planner.EndProperty) as DateTime?; TimeSpan timeSpan = End.Value.AddHours(1) - Start.Value; foreach (UIElement element in Children) { DateTime?startTime = element.GetValue(PlannerAppointmentItem.StartProperty) as DateTime?; DateTime?endTime = element.GetValue(PlannerAppointmentItem.EndProperty) as DateTime?; TimeSpan timeSpanStart = startTime.Value - Start.Value; TimeSpan timeSpanEnd = endTime.Value - Start.Value; double start_minutes = timeSpanStart.TotalMinutes; double end_minutes = timeSpanEnd.TotalMinutes; double start_offset = (finalSize.Width / timeSpan.TotalMinutes) * start_minutes; double end_offset = (finalSize.Width / timeSpan.TotalMinutes) * end_minutes; left = start_offset; top = 1; height = finalSize.Height - 2; width = (end_offset - start_offset); element.Arrange(new Rect(left, top, width, height)); (element as FrameworkElement).Width = width; } return(finalSize); }