123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- 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 DurationEditorControl : DynamicEditorControl<TimeSpan>
- {
- //private DateTimeEdit Editor = null;
- private TimeSpanEdit Editor;
- private bool IsChanged;
- private Button LessButton;
- private Button MoreButton;
- public DurationEditorControl()
- {
- MaxWidth = 260;
- HorizontalAlignment = HorizontalAlignment.Left;
- }
- protected override FrameworkElement CreateEditor()
- {
- var Stack = new DockPanel
- {
- //Orientation = Orientation.Horizontal,
- HorizontalAlignment = HorizontalAlignment.Stretch
- };
- Editor = new TimeSpanEdit
- {
- Format = "h:mm",
- MinValue = new TimeSpan(),
- MaxValue = TimeSpan.MaxValue,
- VerticalAlignment = VerticalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- ShowArrowButtons = 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 TimeSpanEdit).SelectionStart
- // , (o as TimeSpanEdit).SelectionLength
- // , (o as TimeSpanEdit).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.ValueChanged += (o, e) =>
- {
- IsChanged = true;
- //CheckChanged();
- };
- Editor.LostFocus += (o, e) =>
- {
- if (IsChanged)
- CheckChanged();
- };
- LessButton = new Button
- {
- Content = "<",
- Width = 23,
- Margin = new Thickness(2, 0, 0, 0),
- Focusable = false
- };
- LessButton.SetValue(DockPanel.DockProperty, Dock.Right);
- LessButton.Click += (o, e) =>
- {
- Editor.Value = Editor.Value.HasValue && Editor.Value >= new TimeSpan(0, 15, 0)
- ? Editor.Value.Value.Subtract(new TimeSpan(0, 15, 0))
- : new TimeSpan(0);
- CheckChanged();
- };
- MoreButton = new Button
- {
- Content = ">",
- Width = 23,
- Margin = new Thickness(2, 0, 0, 0),
- Focusable = false
- };
- MoreButton.SetValue(DockPanel.DockProperty, Dock.Right);
- MoreButton.Click += (o, e) =>
- {
- Editor.Value = Editor.Value.HasValue ? Editor.Value.Value.Add(new TimeSpan(0, 15, 0)) : new TimeSpan(0, 15, 0);
- CheckChanged();
- };
- Stack.Children.Add(MoreButton);
- Stack.Children.Add(LessButton);
- 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.Value.HasValue)
- result = Editor.Value.Value;
- return result;
- }
- protected override void UpdateValue(TimeSpan value)
- {
- Editor.Value = value;
- }
- 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))
- return;
- var buttons = LessButton != null && MoreButton != null
- ? (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 = (HorizontalAlignment != HorizontalAlignment.Stretch ? Math.Min(width, MaxWidth) : width) - buttons;
- }
- }
- }
|