DateTimeEditorControl.cs 3.1 KB

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