DurationEditorControl.cs 6.3 KB

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