TimeOfDayEditorControl.cs 9.0 KB

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