using System; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Xceed.Wpf.Toolkit; namespace InABox.DynamicGrid { public class DateTimeEditorControl : DynamicEditorControl { private Button Button; private DateTimePicker Editor; protected override FrameworkElement CreateEditor() { var DockPanel = new DockPanel { HorizontalAlignment = HorizontalAlignment.Stretch }; Editor = new DateTimePicker { Format = DateTimeFormat.Custom, FormatString = "dd MMM yyyy HH:mm", VerticalAlignment = VerticalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, CalendarDisplayMode = CalendarMode.Month, ShowButtonSpinner = false }; Editor.GotFocus += (o, e) => { new Timer(s => { Dispatcher.Invoke(() => { Editor.SelectAll(); }); }, Editor, 100, Timeout.Infinite); }; Editor.ValueChanged += (o, e) => { if (Editor.Value.HasValue && Editor.Value.Value.Year < 100) Editor.Value = Editor.Value.Value.AddYears(2000); CheckChanged(); }; Editor.SetValue(DockPanel.DockProperty, Dock.Left); Button = new Button { Content = "Now", Width = 45, Margin = new Thickness(5, 0, 0, 0), Focusable = false }; Button.Click += (o, e) => Editor.Value = DateTime.Today.AddHours(DateTime.Now.TimeOfDay.Hours).AddMinutes(DateTime.Now.TimeOfDay.Minutes); Button.SetValue(DockPanel.DockProperty, Dock.Right); DockPanel.Children.Add(Button); DockPanel.Children.Add(Editor); return DockPanel; } public override int DesiredHeight() { return 25; } public override int DesiredWidth() { return 150 + (int)Button.Width + 5; } protected override DateTime RetrieveValue() { var result = DateTime.MinValue; if (Editor.Value.HasValue) { if (Editor.Value.Value.Year < 100) result = Editor.Value.Value.AddYears(2000); else result = Editor.Value.Value; } result = new DateTime(result.Year, result.Month, result.Day, result.Hour, result.Minute, 0, result.Kind); return result; } protected override void UpdateValue(DateTime value) { if (value != DateTime.MinValue) Editor.Value = value; else Editor.Value = null; } public override void SetFocus() { Editor.Focus(); } public override void SetColor(Color color) { Editor.Background = new SolidColorBrush(color); } } }