TimestampEditorControl.cs 3.1 KB

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