| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 | using System;using System.Windows;using System.Windows.Controls;using System.Windows.Input;using System.Windows.Media;using Syncfusion.Windows.Shared;//using Xceed.Wpf.Toolkit;namespace InABox.DynamicGrid{    public class TimeOfDayEditorControl : DynamicEditorControl<TimeSpan>    {        public static readonly DependencyProperty NowButtonVisibleProperty =            DependencyProperty.Register("NowButtonVisible", typeof(bool), typeof(DynamicSplitPanel), new UIPropertyMetadata(false));        private bool _nowbuttonvisible = true;        private DateTimeEdit Editor;        private bool IsChanged;        private Button LessButton;        private Button MoreButton;        //private TimeSpanEdit Editor = null;        private Button NowButton;        public TimeOfDayEditorControl()        {            MaxWidth = 260;            HorizontalAlignment = HorizontalAlignment.Left;        }        public bool NowButtonVisible        {            get => _nowbuttonvisible;            set            {                _nowbuttonvisible = value;                if (NowButton != null)                    NowButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;                if (LessButton != null)                    LessButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;                if (MoreButton != null)                    MoreButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;            }        }        protected override FrameworkElement CreateEditor()        {            var Stack = new DockPanel            {                //Orientation = Orientation.Horizontal,                HorizontalAlignment = HorizontalAlignment.Stretch            };            Editor = new DateTimeEdit            {                Pattern = DateTimePattern.CustomPattern,                CustomPattern = "HH:mm", //DateTimeFormat.Custom,                //FormatString = "HH:mm",                VerticalAlignment = VerticalAlignment.Stretch,                VerticalContentAlignment = VerticalAlignment.Center,                HorizontalAlignment = HorizontalAlignment.Stretch,                HorizontalContentAlignment = HorizontalAlignment.Center,                //TimeInterval = new TimeSpan(0, 15, 0),                //ShowButtonSpinner = false                DropDownView = DropDownViews.Clock,                IsButtonPopUpEnabled = false            };            Editor.SetValue(DockPanel.DockProperty, Dock.Left);            Editor.PreviewKeyDown += (o, e) =>            {                //Logger.Send(LogType.Information, "", String.Format("DurationEditor:PreviewKeyDown {0} {1} {2} {3}",                //    e.Key.ToString()                //    , (o as DateTimeEdit).SelectionStart                //    , (o as DateTimeEdit).SelectionLength                //    , (o as DateTimeEdit).SelectedText                //));                var separator = Editor.Text.IndexOf(":");                if (e.Key == Key.OemPeriod)                {                    Editor.Select(separator + 1, 2);                    e.Handled = true;                }                else if (e.Key == Key.Back)                {                    if (string.Equals(Editor.SelectedText, "00"))                        Editor.Select(0, separator);                    else                        Editor.SelectedText = "00";                    e.Handled = true;                }                else if (e.Key == Key.Tab)                {                    if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.LeftShift))                    {                        if (Editor.SelectionStart > separator)                        {                            Editor.Select(0, separator);                            e.Handled = true;                        }                    }                    else                    {                        if (Editor.SelectionLength != Editor.Text.Length && Editor.SelectionStart < separator)                        {                            Editor.Select(separator + 1, 2);                            e.Handled = true;                        }                    }                }            };            Editor.DateTimeChanged += (o, e) =>            {                IsChanged = true;                //CheckChanged();            };            Editor.LostFocus += (o, e) =>            {                if (IsChanged)                    CheckChanged();            };            NowButton = new Button            {                Content = "Now",                Width = 45,                Margin = new Thickness(5, 0, 0, 0),                Focusable = false,                Visibility = _nowbuttonvisible ? Visibility.Visible : Visibility.Collapsed            };            NowButton.SetValue(DockPanel.DockProperty, Dock.Right);            NowButton.Click += (o, e) =>            {                Editor.DateTime = DateTime.MinValue.Add(new TimeSpan(DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes, 0));                CheckChanged();            };            LessButton = new Button            {                Content = "<",                Width = 23,                Margin = new Thickness(2, 0, 0, 0),                Focusable = false,                Visibility = _nowbuttonvisible ? Visibility.Collapsed : Visibility.Visible            };            LessButton.SetValue(DockPanel.DockProperty, Dock.Right);            LessButton.Click += (o, e) =>            {                Editor.DateTime = Editor.DateTime.HasValue && Editor.DateTime.Value.TimeOfDay >= new TimeSpan(0, 15, 0)                    ? Editor.DateTime.Value.Subtract(new TimeSpan(0, 15, 0))                    : Editor.DateTime;                CheckChanged();            };            MoreButton = new Button            {                Content = ">",                Width = 23,                Margin = new Thickness(2, 0, 0, 0),                Focusable = false,                Visibility = _nowbuttonvisible ? Visibility.Collapsed : Visibility.Visible            };            MoreButton.SetValue(DockPanel.DockProperty, Dock.Right);            MoreButton.Click += (o, e) =>            {                Editor.DateTime = Editor.DateTime.HasValue && Editor.DateTime.Value.TimeOfDay < new TimeSpan(23, 45, 0)                    ? Editor.DateTime.Value.Add(new TimeSpan(0, 15, 0))                    : Editor.DateTime;                CheckChanged();            };            Stack.Children.Add(MoreButton);            Stack.Children.Add(LessButton);            Stack.Children.Add(NowButton);            Stack.Children.Add(Editor);            return Stack;        }        public override int DesiredHeight()        {            return 25;        }        public override int DesiredWidth()        {            return 150;        }        protected override TimeSpan RetrieveValue()        {            var result = new TimeSpan(0);            if (Editor.DateTime.HasValue)                result = Editor.DateTime.Value.TimeOfDay;            result = new TimeSpan(result.Hours, result.Minutes, 0);            return result;        }        protected override void UpdateValue(TimeSpan value)        {            if (value.Ticks > 0)                Editor.DateTime = DateTime.MinValue.Add(value);            else                Editor.DateTime = null;        }        public override void SetFocus()        {            Editor.Focus();        }        public override void SetColor(Color color)        {            Editor.Background = new SolidColorBrush(color);        }        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)        {            base.OnRenderSizeChanged(sizeInfo);            ResizeEditor(sizeInfo.NewSize.Width);            //if (Editor.ActualWidth > 150.0F)            //    Editor.Width = 150;        }        private void ResizeEditor(double width)        {            if (double.IsNaN(width) || width.Equals(0.0F) || Editor == null)                return;            var buttons = NowButton != null && LessButton != null && MoreButton != null                ? (NowButton.Visibility == Visibility.Visible ? NowButton.ActualWidth + NowButton.Margin.Left + NowButton.Margin.Right : 0F) +                  (LessButton.Visibility == Visibility.Visible ? LessButton.ActualWidth + LessButton.Margin.Left + LessButton.Margin.Right : 0F) +                  (MoreButton.Visibility == Visibility.Visible ? MoreButton.ActualWidth + MoreButton.Margin.Left + MoreButton.Margin.Right : 0F)                : 0.0F;            Editor.Width = Math.Max(0, (HorizontalAlignment != HorizontalAlignment.Stretch ? Math.Min(width, MaxWidth) : width) - buttons);        }    }}
 |