DateEditorControl.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Controls.Primitives;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. using Xceed.Wpf.Toolkit;
  12. namespace InABox.DynamicGrid
  13. {
  14. public class DateEditorControl : DynamicEditorControl<DateTime, DateEditor>
  15. {
  16. static DateEditorControl()
  17. {
  18. //DynamicEditorControlFactory.Register<DateEditorControl, DateEditor>();
  19. }
  20. public static readonly DependencyProperty TodayVisibleProperty =
  21. DependencyProperty.Register(nameof(TodayVisible), typeof(bool), typeof(DateEditorControl));
  22. private Button Button;
  23. private DateTimePicker2 Editor;
  24. public bool TodayVisible
  25. {
  26. get => (bool)GetValue(TodayVisibleProperty);
  27. set
  28. {
  29. SetValue(TodayVisibleProperty, value);
  30. if (Button != null)
  31. Button.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
  32. }
  33. }
  34. public override void Configure()
  35. {
  36. }
  37. protected override FrameworkElement CreateEditor()
  38. {
  39. var DockPanel = new DockPanel
  40. {
  41. HorizontalAlignment = HorizontalAlignment.Stretch
  42. };
  43. Editor = new DateTimePicker2
  44. {
  45. Format = DateTimeFormat.Custom,
  46. FormatString = "dd MMM yyyy",
  47. HorizontalAlignment = HorizontalAlignment.Left,
  48. VerticalAlignment = VerticalAlignment.Stretch,
  49. VerticalContentAlignment = VerticalAlignment.Center,
  50. HorizontalContentAlignment = HorizontalAlignment.Center,
  51. Width = 150,
  52. CalendarDisplayMode = CalendarMode.Month,
  53. TimePickerVisibility = Visibility.Collapsed,
  54. TimePickerShowButtonSpinner = false,
  55. ShowButtonSpinner = false,
  56. AutoCloseCalendar = true,
  57. TextAlignment = TextAlignment.Center
  58. };
  59. Editor.GotFocus += (o, e) => { new Timer(s => { Dispatcher.Invoke(() => { Editor.SelectAll(); }); }, Editor, 100, Timeout.Infinite); };
  60. Editor.ValueChanged += (o, e) =>
  61. {
  62. if (e.OriginalSource != Editor) return;
  63. if (Editor.Value.HasValue && Editor.Value.Value.Year < 100)
  64. Editor.Value = Editor.Value.Value.AddYears(2000);
  65. CheckChanged();
  66. };
  67. Editor.KeyUp += (o, e) =>
  68. {
  69. if (e.Key == Key.Enter || e.Key == Key.Enter)
  70. {
  71. if (Editor.Value.HasValue && Editor.Value.Value.Year < 100)
  72. Editor.Value = Editor.Value.Value.AddYears(2000);
  73. CheckChanged();
  74. }
  75. };
  76. Editor.LostFocus += (o, e) => { };
  77. Editor.MouseDoubleClick += (o, e) => Editor.Value = DateTime.Today;
  78. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  79. var todayvisible = TodayVisible || (EditorDefinition is DateEditor && (EditorDefinition as DateEditor).TodayVisible);
  80. Button = new Button
  81. {
  82. Content = "Today",
  83. Width = 45,
  84. Margin = new Thickness(5, 0, 0, 0),
  85. Focusable = false,
  86. Visibility = todayvisible ? Visibility.Visible : Visibility.Collapsed
  87. };
  88. Button.Click += (o, e) => { Editor.Value = DateTime.Today; };
  89. Button.SetValue(DockPanel.DockProperty, Dock.Right);
  90. DockPanel.Children.Add(Button);
  91. DockPanel.Children.Add(Editor);
  92. return DockPanel;
  93. }
  94. public override int DesiredHeight()
  95. {
  96. return 25;
  97. }
  98. public override int DesiredWidth()
  99. {
  100. return 200;
  101. }
  102. protected override DateTime RetrieveValue()
  103. {
  104. if (Editor.Value.HasValue)
  105. {
  106. if (Editor.Value.Value.Date.Year < 100)
  107. return Editor.Value.Value.Date.AddYears(2000);
  108. return Editor.Value.Value.Date;
  109. }
  110. return DateTime.MinValue;
  111. }
  112. protected override void UpdateValue(DateTime value)
  113. {
  114. if (value != DateTime.MinValue)
  115. Editor.Value = value;
  116. else
  117. Editor.Value = null;
  118. }
  119. public override void SetFocus()
  120. {
  121. Editor.Focus();
  122. }
  123. public override void SetColor(Color color)
  124. {
  125. Editor.Background = new SolidColorBrush(color);
  126. }
  127. private class DateTimePicker2 : DateTimePicker
  128. {
  129. private Calendar _current;
  130. protected override void Popup_Opened(object sender, EventArgs e)
  131. {
  132. base.Popup_Opened(sender, e);
  133. _current = ((Popup)sender).Child.FindVisualChildren<Calendar>().FirstOrDefault();
  134. }
  135. protected override void OnIsOpenChanged(bool oldValue, bool newValue)
  136. {
  137. base.OnIsOpenChanged(oldValue, newValue);
  138. if (oldValue != newValue && newValue == false && _current != null)
  139. Value = _current.SelectedDate;
  140. }
  141. }
  142. }
  143. }