DateEditorControl.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.SetValue(DockPanel.DockProperty, Dock.Left);
  78. var todayvisible = TodayVisible || (EditorDefinition is DateEditor && (EditorDefinition as DateEditor).TodayVisible);
  79. Button = new Button
  80. {
  81. Content = "Today",
  82. Width = 45,
  83. Margin = new Thickness(5, 0, 0, 0),
  84. Focusable = false,
  85. Visibility = todayvisible ? Visibility.Visible : Visibility.Collapsed
  86. };
  87. Button.Click += (o, e) => { Editor.Value = DateTime.Today; };
  88. Button.SetValue(DockPanel.DockProperty, Dock.Right);
  89. DockPanel.Children.Add(Button);
  90. DockPanel.Children.Add(Editor);
  91. return DockPanel;
  92. }
  93. public override int DesiredHeight()
  94. {
  95. return 25;
  96. }
  97. public override int DesiredWidth()
  98. {
  99. return 200;
  100. }
  101. protected override DateTime RetrieveValue()
  102. {
  103. if (Editor.Value.HasValue)
  104. {
  105. if (Editor.Value.Value.Date.Year < 100)
  106. return Editor.Value.Value.Date.AddYears(2000);
  107. return Editor.Value.Value.Date;
  108. }
  109. return DateTime.MinValue;
  110. }
  111. protected override void UpdateValue(DateTime value)
  112. {
  113. if (value != DateTime.MinValue)
  114. Editor.Value = value;
  115. else
  116. Editor.Value = null;
  117. }
  118. public override void SetFocus()
  119. {
  120. Editor.Focus();
  121. }
  122. public override void SetColor(Color color)
  123. {
  124. Editor.Background = new SolidColorBrush(color);
  125. }
  126. private class DateTimePicker2 : DateTimePicker
  127. {
  128. private Calendar _current;
  129. protected override void Popup_Opened(object sender, EventArgs e)
  130. {
  131. base.Popup_Opened(sender, e);
  132. _current = ((Popup)sender).Child.FindVisualChildren<Calendar>().FirstOrDefault();
  133. }
  134. protected override void OnIsOpenChanged(bool oldValue, bool newValue)
  135. {
  136. base.OnIsOpenChanged(oldValue, newValue);
  137. if (oldValue != newValue && newValue == false && _current != null)
  138. Value = _current.SelectedDate;
  139. }
  140. }
  141. }
  142. }