From 4d56cc121e5d5faae3d4848889f9b780169efe5a Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Mon, 19 Nov 2018 10:49:32 +0800 Subject: [PATCH] enhance the function of timepicker --- HandyControl/Controls/Input/NumericUpDown.cs | 6 - HandyControl/Controls/Input/TimePicker.cs | 81 ++++- .../Styles/Base/TimePickerBaseStyle.xaml | 286 +++++++++++++++++- HandyControl/Themes/Styles/TimePicker.xaml | 119 +------- .../Controls/TimePickerDemoCtl.xaml | 25 +- .../UserControl/Main/LeftMainContent.xaml | 10 +- 6 files changed, 390 insertions(+), 137 deletions(-) diff --git a/HandyControl/Controls/Input/NumericUpDown.cs b/HandyControl/Controls/Input/NumericUpDown.cs index f6b70ca5..172bed30 100644 --- a/HandyControl/Controls/Input/NumericUpDown.cs +++ b/HandyControl/Controls/Input/NumericUpDown.cs @@ -5,7 +5,6 @@ using System.Windows.Input; using HandyControl.Data; using HandyControl.Interactivity; -// ReSharper disable once CheckNamespace namespace HandyControl.Controls { /// @@ -27,11 +26,6 @@ namespace HandyControl.Controls #endregion Data - static NumericUpDown() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown))); - } - public NumericUpDown() { CommandBindings.Add(new CommandBinding(ControlCommands.Prev, (s, e) => diff --git a/HandyControl/Controls/Input/TimePicker.cs b/HandyControl/Controls/Input/TimePicker.cs index 47669f96..d9c2648b 100644 --- a/HandyControl/Controls/Input/TimePicker.cs +++ b/HandyControl/Controls/Input/TimePicker.cs @@ -7,8 +7,8 @@ using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Threading; using HandyControl.Data; +using HandyControl.Interactivity; -// ReSharper disable once CheckNamespace namespace HandyControl.Controls { /// @@ -19,7 +19,7 @@ namespace HandyControl.Controls [TemplatePart(Name = ElementButton, Type = typeof(Button))] [TemplatePart(Name = ElementPopup, Type = typeof(Popup))] [TemplatePart(Name = ElementClock, Type = typeof(Clock))] - public class TimePicker : Control + public class TimePicker : Control, IDataInput { #region Constants @@ -69,6 +69,15 @@ namespace HandyControl.Controls KeyboardNavigation.IsTabStopProperty.OverrideMetadata(typeof(TimePicker), new FrameworkPropertyMetadata(false)); } + public TimePicker() + { + CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) => + { + ClearValue(SelectedTimeProperty); + ClearValue(TextProperty); + })); + } + #region Public Properties #region TimeFormat @@ -249,10 +258,77 @@ namespace HandyControl.Controls #endregion Text + public Func> VerifyFunc { get; set; } + + public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register( + "IsError", typeof(bool), typeof(TimePicker), new PropertyMetadata(ValueBoxes.FalseBox)); + + public bool IsError + { + get => (bool) GetValue(IsErrorProperty); + set => SetValue(IsErrorProperty, value); + } + + public static readonly DependencyProperty ErrorStrProperty = DependencyProperty.Register( + "ErrorStr", typeof(string), typeof(TimePicker), new PropertyMetadata(default(string))); + + public string ErrorStr + { + get => (string) GetValue(ErrorStrProperty); + set => SetValue(ErrorStrProperty, value); + } + + public static readonly DependencyProperty TextTypeProperty = DependencyProperty.Register( + "TextType", typeof(TextType), typeof(TimePicker), new PropertyMetadata(default(TextType))); + + public TextType TextType + { + get => (TextType) GetValue(TextTypeProperty); + set => SetValue(TextTypeProperty, value); + } + + public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.Register( + "ShowClearButton", typeof(bool), typeof(TimePicker), new PropertyMetadata(ValueBoxes.FalseBox)); + + public bool ShowClearButton + { + get => (bool) GetValue(ShowClearButtonProperty); + set => SetValue(ShowClearButtonProperty, value); + } + #endregion Public Properties #region Public Methods + public virtual bool VerifyData() + { + OperationResult result; + + if (VerifyFunc != null) + { + result = VerifyFunc.Invoke(Text); + } + else + { + if (!string.IsNullOrEmpty(Text)) + { + result = OperationResult.Success(); + } + else if (InfoElement.GetNecessary(this)) + { + result = OperationResult.Failed(Properties.Langs.Lang.IsNecessary); + } + else + { + result = OperationResult.Success(); + } + } + + IsError = !result.Data; + ErrorStr = result.Message; + return result.Data; + } + public override void OnApplyTemplate() { if (DesignerProperties.GetIsInDesignMode(this)) return; @@ -402,6 +478,7 @@ namespace HandyControl.Controls private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { SetValueNoCallback(TextProperty, _textBox.Text); + VerifyData(); } private bool ProcessTimePickerKey(KeyEventArgs e) diff --git a/HandyControl/Themes/Styles/Base/TimePickerBaseStyle.xaml b/HandyControl/Themes/Styles/Base/TimePickerBaseStyle.xaml index c37a79ed..9c468abe 100644 --- a/HandyControl/Themes/Styles/Base/TimePickerBaseStyle.xaml +++ b/HandyControl/Themes/Styles/Base/TimePickerBaseStyle.xaml @@ -1,9 +1,12 @@  + xmlns:controls="clr-namespace:HandyControl.Controls" + xmlns:system="clr-namespace:System;assembly=mscorlib" + xmlns:interactivity="clr-namespace:HandyControl.Interactivity"> + @@ -61,4 +64,285 @@ + + + + + + + + + + + + + + + + +