DateTimeEditorControl.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. Width=150
  37. };
  38. Editor.GotFocus += (o, e) => { new Timer(s => { Dispatcher.Invoke(() => { Editor.SelectAll(); }); }, Editor, 100, Timeout.Infinite); };
  39. Editor.ValueChanged += (o, e) =>
  40. {
  41. if (Editor.Value.HasValue && Editor.Value.Value.Year < 100)
  42. Editor.Value = Editor.Value.Value.AddYears(2000);
  43. CheckChanged();
  44. };
  45. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  46. Button = new Button
  47. {
  48. Content = "Now",
  49. Width = 45,
  50. Margin = new Thickness(5, 0, 0, 0),
  51. Focusable = false
  52. };
  53. Button.Click += (o, e) => Editor.Value = DateTime.Today.AddHours(DateTime.Now.TimeOfDay.Hours).AddMinutes(DateTime.Now.TimeOfDay.Minutes);
  54. Button.SetValue(DockPanel.DockProperty, Dock.Right);
  55. DockPanel.Children.Add(Button);
  56. DockPanel.Children.Add(Editor);
  57. return DockPanel;
  58. }
  59. public override int DesiredHeight()
  60. {
  61. return 25;
  62. }
  63. public override int DesiredWidth()
  64. {
  65. return 150 + (int)Button.Width + 5;
  66. }
  67. protected override DateTime RetrieveValue()
  68. {
  69. var result = DateTime.MinValue;
  70. if (Editor.Value.HasValue)
  71. {
  72. if (Editor.Value.Value.Year < 100)
  73. result = Editor.Value.Value.AddYears(2000);
  74. else
  75. result = Editor.Value.Value;
  76. }
  77. return result;
  78. }
  79. protected override void UpdateValue(DateTime value)
  80. {
  81. if (value != DateTime.MinValue)
  82. Editor.Value = value;
  83. else
  84. Editor.Value = null;
  85. }
  86. public override void SetFocus()
  87. {
  88. Editor.Focus();
  89. }
  90. public override void SetColor(Color color)
  91. {
  92. Editor.Background = new SolidColorBrush(color);
  93. }
  94. }
  95. }