TimeOfDayEditorControl.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using Syncfusion.Windows.Shared;
  7. //using Xceed.Wpf.Toolkit;
  8. namespace InABox.DynamicGrid
  9. {
  10. public class TimeOfDayEditorControl : DynamicEditorControl<TimeSpan>
  11. {
  12. public static readonly DependencyProperty NowButtonVisibleProperty =
  13. DependencyProperty.Register("NowButtonVisible", typeof(bool), typeof(DynamicSplitPanel), new UIPropertyMetadata(false));
  14. private bool _nowbuttonvisible = true;
  15. private DateTimeEdit Editor;
  16. private bool IsChanged;
  17. private Button LessButton;
  18. private Button MoreButton;
  19. //private TimeSpanEdit Editor = null;
  20. private Button NowButton;
  21. public TimeOfDayEditorControl()
  22. {
  23. MaxWidth = 260;
  24. HorizontalAlignment = HorizontalAlignment.Left;
  25. }
  26. public bool NowButtonVisible
  27. {
  28. get => _nowbuttonvisible;
  29. set
  30. {
  31. _nowbuttonvisible = value;
  32. if (NowButton != null)
  33. NowButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
  34. if (LessButton != null)
  35. LessButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
  36. if (MoreButton != null)
  37. MoreButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
  38. }
  39. }
  40. protected override FrameworkElement CreateEditor()
  41. {
  42. var Stack = new DockPanel
  43. {
  44. //Orientation = Orientation.Horizontal,
  45. HorizontalAlignment = HorizontalAlignment.Stretch
  46. };
  47. Editor = new DateTimeEdit
  48. {
  49. Pattern = DateTimePattern.CustomPattern,
  50. CustomPattern = "HH:mm", //DateTimeFormat.Custom,
  51. //FormatString = "HH:mm",
  52. VerticalAlignment = VerticalAlignment.Stretch,
  53. VerticalContentAlignment = VerticalAlignment.Center,
  54. HorizontalAlignment = HorizontalAlignment.Stretch,
  55. HorizontalContentAlignment = HorizontalAlignment.Center,
  56. //TimeInterval = new TimeSpan(0, 15, 0),
  57. //ShowButtonSpinner = false
  58. DropDownView = DropDownViews.Clock,
  59. IsButtonPopUpEnabled = false
  60. };
  61. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  62. Editor.PreviewKeyDown += (o, e) =>
  63. {
  64. //Logger.Send(LogType.Information, "", String.Format("DurationEditor:PreviewKeyDown {0} {1} {2} {3}",
  65. // e.Key.ToString()
  66. // , (o as DateTimeEdit).SelectionStart
  67. // , (o as DateTimeEdit).SelectionLength
  68. // , (o as DateTimeEdit).SelectedText
  69. //));
  70. var separator = Editor.Text.IndexOf(":");
  71. if (e.Key == Key.OemPeriod)
  72. {
  73. Editor.Select(separator + 1, 2);
  74. e.Handled = true;
  75. }
  76. else if (e.Key == Key.Back)
  77. {
  78. if (string.Equals(Editor.SelectedText, "00"))
  79. Editor.Select(0, separator);
  80. else
  81. Editor.SelectedText = "00";
  82. e.Handled = true;
  83. }
  84. else if (e.Key == Key.Tab)
  85. {
  86. if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.LeftShift))
  87. {
  88. if (Editor.SelectionStart > separator)
  89. {
  90. Editor.Select(0, separator);
  91. e.Handled = true;
  92. }
  93. }
  94. else
  95. {
  96. if (Editor.SelectionLength != Editor.Text.Length && Editor.SelectionStart < separator)
  97. {
  98. Editor.Select(separator + 1, 2);
  99. e.Handled = true;
  100. }
  101. }
  102. }
  103. };
  104. Editor.DateTimeChanged += (o, e) =>
  105. {
  106. IsChanged = true;
  107. //CheckChanged();
  108. };
  109. Editor.LostFocus += (o, e) =>
  110. {
  111. if (IsChanged)
  112. CheckChanged();
  113. };
  114. NowButton = new Button
  115. {
  116. Content = "Now",
  117. Width = 45,
  118. Margin = new Thickness(5, 0, 0, 0),
  119. Focusable = false,
  120. Visibility = _nowbuttonvisible ? Visibility.Visible : Visibility.Collapsed
  121. };
  122. NowButton.SetValue(DockPanel.DockProperty, Dock.Right);
  123. NowButton.Click += (o, e) =>
  124. {
  125. Editor.DateTime = DateTime.MinValue.Add(new TimeSpan(DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes, 0));
  126. CheckChanged();
  127. };
  128. LessButton = new Button
  129. {
  130. Content = "<",
  131. Width = 23,
  132. Margin = new Thickness(2, 0, 0, 0),
  133. Focusable = false,
  134. Visibility = _nowbuttonvisible ? Visibility.Collapsed : Visibility.Visible
  135. };
  136. LessButton.SetValue(DockPanel.DockProperty, Dock.Right);
  137. LessButton.Click += (o, e) =>
  138. {
  139. Editor.DateTime = Editor.DateTime.HasValue && Editor.DateTime.Value.TimeOfDay >= new TimeSpan(0, 15, 0)
  140. ? Editor.DateTime.Value.Subtract(new TimeSpan(0, 15, 0))
  141. : Editor.DateTime;
  142. CheckChanged();
  143. };
  144. MoreButton = new Button
  145. {
  146. Content = ">",
  147. Width = 23,
  148. Margin = new Thickness(2, 0, 0, 0),
  149. Focusable = false,
  150. Visibility = _nowbuttonvisible ? Visibility.Collapsed : Visibility.Visible
  151. };
  152. MoreButton.SetValue(DockPanel.DockProperty, Dock.Right);
  153. MoreButton.Click += (o, e) =>
  154. {
  155. Editor.DateTime = Editor.DateTime.HasValue && Editor.DateTime.Value.TimeOfDay < new TimeSpan(23, 45, 0)
  156. ? Editor.DateTime.Value.Add(new TimeSpan(0, 15, 0))
  157. : Editor.DateTime;
  158. CheckChanged();
  159. };
  160. Stack.Children.Add(MoreButton);
  161. Stack.Children.Add(LessButton);
  162. Stack.Children.Add(NowButton);
  163. Stack.Children.Add(Editor);
  164. return Stack;
  165. }
  166. public override int DesiredHeight()
  167. {
  168. return 25;
  169. }
  170. public override int DesiredWidth()
  171. {
  172. return 150;
  173. }
  174. protected override TimeSpan RetrieveValue()
  175. {
  176. var result = new TimeSpan(0);
  177. if (Editor.DateTime.HasValue)
  178. result = Editor.DateTime.Value.TimeOfDay;
  179. result = new TimeSpan(result.Hours, result.Minutes, 0);
  180. return result;
  181. }
  182. protected override void UpdateValue(TimeSpan value)
  183. {
  184. if (value.Ticks > 0)
  185. Editor.DateTime = DateTime.MinValue.Add(value);
  186. else
  187. Editor.DateTime = null;
  188. }
  189. public override void SetFocus()
  190. {
  191. Editor.Focus();
  192. }
  193. public override void SetColor(Color color)
  194. {
  195. Editor.Background = new SolidColorBrush(color);
  196. }
  197. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  198. {
  199. base.OnRenderSizeChanged(sizeInfo);
  200. ResizeEditor(sizeInfo.NewSize.Width);
  201. //if (Editor.ActualWidth > 150.0F)
  202. // Editor.Width = 150;
  203. }
  204. private void ResizeEditor(double width)
  205. {
  206. if (double.IsNaN(width) || width.Equals(0.0F) || Editor == null)
  207. return;
  208. var buttons = NowButton != null && LessButton != null && MoreButton != null
  209. ? (NowButton.Visibility == Visibility.Visible ? NowButton.ActualWidth + NowButton.Margin.Left + NowButton.Margin.Right : 0F) +
  210. (LessButton.Visibility == Visibility.Visible ? LessButton.ActualWidth + LessButton.Margin.Left + LessButton.Margin.Right : 0F) +
  211. (MoreButton.Visibility == Visibility.Visible ? MoreButton.ActualWidth + MoreButton.Margin.Left + MoreButton.Margin.Right : 0F)
  212. : 0.0F;
  213. Editor.Width = Math.Max(0, (HorizontalAlignment != HorizontalAlignment.Stretch ? Math.Min(width, MaxWidth) : width) - buttons);
  214. }
  215. }
  216. }