DateTimeEditorControl.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Threading;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using InABox.Core;
  7. using Xceed.Wpf.Toolkit;
  8. namespace InABox.DynamicGrid
  9. {
  10. public class DateTimeEditorControl : DynamicEditorControl<DateTime, DateTimeEditor>
  11. {
  12. static DateTimeEditorControl()
  13. {
  14. //DynamicEditorControlFactory.Register<DateTimeEditorControl, DateTimeEditor>();
  15. }
  16. private Button Button;
  17. private DateTimePicker Editor;
  18. public override void Configure()
  19. {
  20. }
  21. protected override FrameworkElement CreateEditor()
  22. {
  23. var DockPanel = new DockPanel
  24. {
  25. HorizontalAlignment = HorizontalAlignment.Stretch
  26. };
  27. Editor = new DateTimePicker
  28. {
  29. Format = DateTimeFormat.Custom,
  30. FormatString = "dd MMM yyyy HH:mm",
  31. VerticalAlignment = VerticalAlignment.Stretch,
  32. VerticalContentAlignment = VerticalAlignment.Center,
  33. HorizontalContentAlignment = HorizontalAlignment.Center,
  34. CalendarDisplayMode = CalendarMode.Month,
  35. ShowButtonSpinner = false
  36. };
  37. Editor.GotFocus += (o, e) => { new Timer(s => { Dispatcher.Invoke(() => { Editor.SelectAll(); }); }, Editor, 100, Timeout.Infinite); };
  38. Editor.ValueChanged += (o, e) =>
  39. {
  40. if (Editor.Value.HasValue && Editor.Value.Value.Year < 100)
  41. Editor.Value = Editor.Value.Value.AddYears(2000);
  42. CheckChanged();
  43. };
  44. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  45. Button = new Button
  46. {
  47. Content = "Now",
  48. Width = 45,
  49. Margin = new Thickness(5, 0, 0, 0),
  50. Focusable = false
  51. };
  52. Button.Click += (o, e) => Editor.Value = DateTime.Today.AddHours(DateTime.Now.TimeOfDay.Hours).AddMinutes(DateTime.Now.TimeOfDay.Minutes);
  53. Button.SetValue(DockPanel.DockProperty, Dock.Right);
  54. DockPanel.Children.Add(Button);
  55. DockPanel.Children.Add(Editor);
  56. return DockPanel;
  57. }
  58. public override int DesiredHeight()
  59. {
  60. return 25;
  61. }
  62. public override int DesiredWidth()
  63. {
  64. return 150 + (int)Button.Width + 5;
  65. }
  66. protected override DateTime RetrieveValue()
  67. {
  68. var result = DateTime.MinValue;
  69. if (Editor.Value.HasValue)
  70. {
  71. if (Editor.Value.Value.Year < 100)
  72. result = Editor.Value.Value.AddYears(2000);
  73. else
  74. result = Editor.Value.Value;
  75. }
  76. return result;
  77. }
  78. protected override void UpdateValue(DateTime value)
  79. {
  80. if (value != DateTime.MinValue)
  81. Editor.Value = value;
  82. else
  83. Editor.Value = null;
  84. }
  85. public override void SetFocus()
  86. {
  87. Editor.Focus();
  88. }
  89. public override void SetColor(Color color)
  90. {
  91. Editor.Background = new SolidColorBrush(color);
  92. }
  93. }
  94. }