DateEditorControl.cs 5.0 KB

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