DurationEditorControl.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 DurationEditorControl : DynamicEditorControl<TimeSpan, DurationEditor>
  12. {
  13. static DurationEditorControl()
  14. {
  15. //DynamicEditorControlFactory.Register<DurationEditorControl, DurationEditor>();
  16. }
  17. //private DateTimeEdit Editor = null;
  18. private TimeSpanEdit Editor;
  19. private bool IsChanged;
  20. private Button LessButton;
  21. private Button MoreButton;
  22. public override void Configure()
  23. {
  24. }
  25. public DurationEditorControl()
  26. {
  27. MaxWidth = 260;
  28. HorizontalAlignment = HorizontalAlignment.Left;
  29. }
  30. protected override FrameworkElement CreateEditor()
  31. {
  32. var Stack = new DockPanel
  33. {
  34. //Orientation = Orientation.Horizontal,
  35. HorizontalAlignment = HorizontalAlignment.Stretch
  36. };
  37. Editor = new TimeSpanEdit
  38. {
  39. Format = "h:mm",
  40. MinValue = new TimeSpan(),
  41. MaxValue = TimeSpan.MaxValue,
  42. VerticalAlignment = VerticalAlignment.Stretch,
  43. VerticalContentAlignment = VerticalAlignment.Center,
  44. HorizontalAlignment = HorizontalAlignment.Stretch,
  45. HorizontalContentAlignment = HorizontalAlignment.Center,
  46. ShowArrowButtons = false
  47. };
  48. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  49. Editor.PreviewKeyDown += (o, e) =>
  50. {
  51. //Logger.Send(LogType.Information, "", String.Format("DurationEditor:PreviewKeyDown {0} {1} {2} {3}",
  52. // e.Key.ToString()
  53. // , (o as TimeSpanEdit).SelectionStart
  54. // , (o as TimeSpanEdit).SelectionLength
  55. // , (o as TimeSpanEdit).SelectedText
  56. //));
  57. var separator = Editor.Text.IndexOf(":");
  58. if (e.Key == Key.OemPeriod)
  59. {
  60. Editor.Select(separator + 1, 2);
  61. e.Handled = true;
  62. }
  63. else if (e.Key == Key.Back)
  64. {
  65. if (string.Equals(Editor.SelectedText, "00"))
  66. Editor.Select(0, separator);
  67. else
  68. Editor.SelectedText = "00";
  69. e.Handled = true;
  70. }
  71. else if (e.Key == Key.Tab)
  72. {
  73. if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.LeftShift))
  74. {
  75. if (Editor.SelectionStart > separator)
  76. {
  77. Editor.Select(0, separator);
  78. e.Handled = true;
  79. }
  80. }
  81. else
  82. {
  83. if (Editor.SelectionLength != Editor.Text.Length && Editor.SelectionStart < separator)
  84. {
  85. Editor.Select(separator + 1, 2);
  86. e.Handled = true;
  87. }
  88. }
  89. }
  90. };
  91. Editor.ValueChanged += (o, e) =>
  92. {
  93. IsChanged = true;
  94. CheckChanged();
  95. };
  96. Editor.LostFocus += (o, e) =>
  97. {
  98. if (IsChanged)
  99. CheckChanged();
  100. };
  101. LessButton = new Button
  102. {
  103. Content = "<",
  104. Width = 23,
  105. Margin = new Thickness(2, 0, 0, 0),
  106. Focusable = false
  107. };
  108. LessButton.SetValue(DockPanel.DockProperty, Dock.Right);
  109. LessButton.Click += (o, e) =>
  110. {
  111. Editor.Value = Editor.Value.HasValue && Editor.Value >= new TimeSpan(0, 15, 0)
  112. ? Editor.Value.Value.Subtract(new TimeSpan(0, 15, 0))
  113. : new TimeSpan(0);
  114. CheckChanged();
  115. };
  116. MoreButton = new Button
  117. {
  118. Content = ">",
  119. Width = 23,
  120. Margin = new Thickness(2, 0, 0, 0),
  121. Focusable = false
  122. };
  123. MoreButton.SetValue(DockPanel.DockProperty, Dock.Right);
  124. MoreButton.Click += (o, e) =>
  125. {
  126. Editor.Value = Editor.Value.HasValue ? Editor.Value.Value.Add(new TimeSpan(0, 15, 0)) : new TimeSpan(0, 15, 0);
  127. CheckChanged();
  128. };
  129. Stack.Children.Add(MoreButton);
  130. Stack.Children.Add(LessButton);
  131. Stack.Children.Add(Editor);
  132. return Stack;
  133. }
  134. public override int DesiredHeight()
  135. {
  136. return 25;
  137. }
  138. public override int DesiredWidth()
  139. {
  140. return 150;
  141. }
  142. protected override TimeSpan RetrieveValue()
  143. {
  144. var result = new TimeSpan(0);
  145. if (Editor.Value.HasValue)
  146. result = Editor.Value.Value;
  147. return result;
  148. }
  149. protected override void UpdateValue(TimeSpan value)
  150. {
  151. Editor.Value = value;
  152. }
  153. public override void SetFocus()
  154. {
  155. Editor.Focus();
  156. }
  157. public override void SetColor(Color color)
  158. {
  159. Editor.Background = new SolidColorBrush(color);
  160. }
  161. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  162. {
  163. base.OnRenderSizeChanged(sizeInfo);
  164. ResizeEditor(sizeInfo.NewSize.Width);
  165. //if (Editor.ActualWidth > 150.0F)
  166. // Editor.Width = 150;
  167. }
  168. private void ResizeEditor(double width)
  169. {
  170. if (double.IsNaN(width) || width.Equals(0.0F))
  171. return;
  172. var buttons = LessButton != null && MoreButton != null
  173. ? (LessButton.Visibility == Visibility.Visible ? LessButton.ActualWidth + LessButton.Margin.Left + LessButton.Margin.Right : 0F) +
  174. (MoreButton.Visibility == Visibility.Visible ? MoreButton.ActualWidth + MoreButton.Margin.Left + MoreButton.Margin.Right : 0F)
  175. : 0.0F;
  176. Editor.Width = (HorizontalAlignment != HorizontalAlignment.Stretch ? Math.Min(width, MaxWidth) : width) - buttons;
  177. }
  178. }
  179. }