DateEditorControl.cs 5.3 KB

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