TimestampEditorControl.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using InABox.Core;
  2. using System;
  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 TimestampEditorControl : DynamicEditorControl<DateTime, TimestampEditor>
  10. {
  11. static TimestampEditorControl()
  12. {
  13. //DynamicEditorControlFactory.Register<TimestampEditorControl, TimestampEditor>();
  14. }
  15. private Button Button;
  16. private DateTimePicker Editor;
  17. public override void Configure()
  18. {
  19. }
  20. protected override FrameworkElement CreateEditor()
  21. {
  22. var DockPanel = new DockPanel
  23. {
  24. HorizontalAlignment = HorizontalAlignment.Stretch
  25. };
  26. Editor = new DateTimePicker
  27. {
  28. Format = DateTimeFormat.Custom,
  29. FormatString = "dd MMM yyyy HH:mm:ss",
  30. HorizontalAlignment = HorizontalAlignment.Left,
  31. VerticalAlignment = VerticalAlignment.Stretch,
  32. VerticalContentAlignment = VerticalAlignment.Center,
  33. HorizontalContentAlignment = HorizontalAlignment.Center,
  34. ShowButtonSpinner = false,
  35. //ShowDropDownButton = false,
  36. Focusable = false,
  37. IsReadOnly = true,
  38. Background = new SolidColorBrush(Colors.Gainsboro),
  39. Width = 150
  40. };
  41. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  42. Editor.ValueChanged += (o, e) => CheckChanged();
  43. Button = new Button
  44. {
  45. Content = "Set",
  46. Width = 45,
  47. Margin = new Thickness(5, 0, 0, 0),
  48. Focusable = true
  49. };
  50. Button.SetValue(DockPanel.DockProperty, Dock.Right);
  51. Button.Click += ButtonClick;
  52. DockPanel.Children.Add(Button);
  53. DockPanel.Children.Add(Editor);
  54. return DockPanel;
  55. }
  56. private void ButtonClick(object sender, RoutedEventArgs e)
  57. {
  58. if (((string)Button.Content).Equals("Set"))
  59. Editor.Value = DateTime.Now;
  60. else
  61. Editor.Value = null;
  62. Button.Content = Editor.Value.HasValue ? "Clear" : "Set";
  63. }
  64. public override int DesiredHeight()
  65. {
  66. return 25;
  67. }
  68. public override int DesiredWidth()
  69. {
  70. return 150 + (int)Button.Width + 5;
  71. }
  72. protected override DateTime RetrieveValue()
  73. {
  74. if (Editor.Value.HasValue)
  75. return Editor.Value.Value;
  76. return DateTime.MinValue;
  77. }
  78. protected override void UpdateValue(DateTime value)
  79. {
  80. if (value != DateTime.MinValue)
  81. {
  82. Editor.Value = value;
  83. Button.Content = "Clear"; // Editor.Value.Equals(DateTime.MinValue) ? "Set" : "Clear";
  84. }
  85. else
  86. {
  87. Editor.Value = null;
  88. Button.Content = "Set";
  89. }
  90. }
  91. public override void SetFocus()
  92. {
  93. Button.Focus();
  94. }
  95. public override void SetColor(Color color)
  96. {
  97. //Editor.Background = new SolidColorBrush(color);
  98. }
  99. }
  100. }