From bc2bd5413c840e0861ad1ec83e3568b4688c9cfc Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Thu, 8 Nov 2018 14:28:23 +0800 Subject: [PATCH] add TextBox control now you can clear and verify the text of textbox --- HandyControl/Controls/Clock/Clock.cs | 13 +- .../Controls/{Picker => }/ColorPicker.cs | 7 +- HandyControl/Controls/IDataInput.cs | 23 ---- HandyControl/Controls/Input/IDataInput.cs | 43 +++++++ .../Controls/{ => Input}/NumericUpDown.cs | 28 ++--- HandyControl/Controls/Input/TextBox.cs | 113 +++++++++++++++++- .../Controls/{Picker => Input}/TimePicker.cs | 0 HandyControl/Controls/Loading/LoadingLine.cs | 3 - HandyControl/Controls/Steps/StepBar.cs | 6 + HandyControl/Controls/TimeBar/TimeBar.cs | 8 +- HandyControl/HandyControl.csproj | 8 +- HandyControl/Themes/Styles/Slider.xaml | 10 +- HandyControl/Themes/Styles/TextBox.xaml | 103 +++++++++++++--- HandyControlDemo/Data/MessageToken.cs | 4 +- HandyControlDemo/Data/MessageToken.tt | 5 +- HandyControlDemo/HandyControlDemo.csproj | 13 +- .../Properties/Langs/Lang.Designer.cs | 9 ++ .../Properties/Langs/Lang.en.resx | 3 + HandyControlDemo/Properties/Langs/Lang.resx | 3 + .../Controls/AnimationPathDemoCtl.xaml.cs | 3 - .../Controls/CarouselDemoCtl.xaml.cs | 3 - .../UserControl/Controls/ClockDemoCtl.xaml.cs | 3 - .../Controls/ColorPickerDemoCtl.xaml.cs | 3 - .../Controls/CompareSliderDemoCtl.xaml.cs | 3 - .../Controls/LoadingDemoCtl.xaml.cs | 3 - .../Controls/PaginationDemoCtl.xaml.cs | 3 - .../Controls/TabControlDemoCtl.xaml.cs | 3 - .../UserControl/Controls/TextBoxDemoCtl.xaml | 18 +++ .../TextBoxDemoCtl.xaml.cs | 3 - .../Controls/TimeBarDemoCtl.xaml.cs | 3 - .../UserControl/Main/LeftMainContent.xaml | 5 + .../Styles/NaiveTextBoxDemoCtl.xaml | 17 +++ .../Styles/NaiveTextBoxDemoCtl.xaml.cs | 11 ++ .../Styles/ProgressBarDemoCtl.xaml.cs | 3 - .../UserControl/Styles/TextBoxDemoCtl.xaml | 18 --- 35 files changed, 369 insertions(+), 135 deletions(-) rename HandyControl/Controls/{Picker => }/ColorPicker.cs (99%) delete mode 100644 HandyControl/Controls/IDataInput.cs create mode 100644 HandyControl/Controls/Input/IDataInput.cs rename HandyControl/Controls/{ => Input}/NumericUpDown.cs (96%) rename HandyControl/Controls/{Picker => Input}/TimePicker.cs (100%) create mode 100644 HandyControlDemo/UserControl/Controls/TextBoxDemoCtl.xaml rename HandyControlDemo/UserControl/{Styles => Controls}/TextBoxDemoCtl.xaml.cs (73%) create mode 100644 HandyControlDemo/UserControl/Styles/NaiveTextBoxDemoCtl.xaml create mode 100644 HandyControlDemo/UserControl/Styles/NaiveTextBoxDemoCtl.xaml.cs delete mode 100644 HandyControlDemo/UserControl/Styles/TextBoxDemoCtl.xaml diff --git a/HandyControl/Controls/Clock/Clock.cs b/HandyControl/Controls/Clock/Clock.cs index ec82730a..cf7707bb 100644 --- a/HandyControl/Controls/Clock/Clock.cs +++ b/HandyControl/Controls/Clock/Clock.cs @@ -98,7 +98,7 @@ namespace HandyControl.Controls #region Public Properties public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register( - "SelectedTime", typeof(DateTime?), typeof(Clock), new PropertyMetadata(default(DateTime), OnSelectedTimeChanged)); + "SelectedTime", typeof(DateTime?), typeof(Clock), new PropertyMetadata(default(DateTime?), OnSelectedTimeChanged)); private static void OnSelectedTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { @@ -239,6 +239,17 @@ namespace HandyControl.Controls } _appliedTemplate = true; + if (_isLoaded) + { + if (SelectedTime.HasValue) + { + Update(SelectedTime.Value); + } + else + { + DisplayTime = DateTime.Now; + } + } } #endregion Public Methods diff --git a/HandyControl/Controls/Picker/ColorPicker.cs b/HandyControl/Controls/ColorPicker.cs similarity index 99% rename from HandyControl/Controls/Picker/ColorPicker.cs rename to HandyControl/Controls/ColorPicker.cs index 87448489..51a426ec 100644 --- a/HandyControl/Controls/Picker/ColorPicker.cs +++ b/HandyControl/Controls/ColorPicker.cs @@ -9,7 +9,6 @@ using HandyControl.Data; using HandyControl.Interactivity; using HandyControl.Tools.Extension; -// ReSharper disable once CheckNamespace namespace HandyControl.Controls { /// @@ -359,6 +358,8 @@ namespace HandyControl.Controls _sliderOpacity.ValueChanged -= SliderOpacity_OnValueChanged; } + _panelColor?.Children.Clear(); + _panelRgb?.RemoveHandler(NumericUpDown.ValueChangedEvent, new EventHandler>(NumericUpDownRgb_OnValueChanged)); base.OnApplyTemplate(); @@ -391,6 +392,10 @@ namespace HandyControl.Controls collection.Add(behavior); } + if (_isLoaded) + { + Init(); + } _panelRgb?.AddHandler(NumericUpDown.ValueChangedEvent, new EventHandler>(NumericUpDownRgb_OnValueChanged)); } diff --git a/HandyControl/Controls/IDataInput.cs b/HandyControl/Controls/IDataInput.cs deleted file mode 100644 index 66f39b04..00000000 --- a/HandyControl/Controls/IDataInput.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using HandyControl.Data; - -namespace HandyControl.Controls -{ - /// - /// 表示可以成为一个数据输入控件 - /// - public interface IDataInput - { - bool VerifyData(); - - Func> VerifyFunc { get; set; } - - bool IsError { get; set; } - - string ErrorStr { get; set; } - - TextType TextType { get; set; } - - bool ShowClearButton { get; set; } - } -} \ No newline at end of file diff --git a/HandyControl/Controls/Input/IDataInput.cs b/HandyControl/Controls/Input/IDataInput.cs new file mode 100644 index 00000000..8ea26c94 --- /dev/null +++ b/HandyControl/Controls/Input/IDataInput.cs @@ -0,0 +1,43 @@ +using System; +using HandyControl.Data; + +// ReSharper disable once CheckNamespace +namespace HandyControl.Controls +{ + /// + /// 表示可以成为一个数据输入控件 + /// + public interface IDataInput + { + /// + /// 验证数据 + /// + /// + bool VerifyData(); + + /// + /// 数据验证委托 + /// + Func> VerifyFunc { get; set; } + + /// + /// 数据是否错误 + /// + bool IsError { get; set; } + + /// + /// 错误提示 + /// + string ErrorStr { get; set; } + + /// + /// 文本类型 + /// + TextType TextType { get; set; } + + /// + /// 是否显示清除按钮 + /// + bool ShowClearButton { get; set; } + } +} \ No newline at end of file diff --git a/HandyControl/Controls/NumericUpDown.cs b/HandyControl/Controls/Input/NumericUpDown.cs similarity index 96% rename from HandyControl/Controls/NumericUpDown.cs rename to HandyControl/Controls/Input/NumericUpDown.cs index 6fe737a1..54fa7130 100644 --- a/HandyControl/Controls/NumericUpDown.cs +++ b/HandyControl/Controls/Input/NumericUpDown.cs @@ -5,8 +5,10 @@ using System.Windows.Input; using HandyControl.Data; using HandyControl.Interactivity; +// ReSharper disable once CheckNamespace namespace HandyControl.Controls { + /// /// /// 数值选择控件 /// @@ -25,6 +27,11 @@ 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) => @@ -44,6 +51,8 @@ namespace HandyControl.Controls ClearValue(ValueProperty); _textBox.Text = string.Empty; })); + + Loaded += (s, e) => OnApplyTemplate(); } protected override void OnGotFocus(RoutedEventArgs e) @@ -309,9 +318,6 @@ namespace HandyControl.Controls public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register( "IsError", typeof(bool), typeof(NumericUpDown), new PropertyMetadata(default(bool))); - /// - /// 数据是否错误 - /// public bool IsError { get => (bool) GetValue(IsErrorProperty); @@ -324,9 +330,6 @@ namespace HandyControl.Controls public static readonly DependencyProperty ErrorStrProperty = DependencyProperty.Register( "ErrorStr", typeof(string), typeof(NumericUpDown), new PropertyMetadata(default(string))); - /// - /// 错误提示 - /// public string ErrorStr { get => (string) GetValue(ErrorStrProperty); @@ -339,9 +342,6 @@ namespace HandyControl.Controls public static readonly DependencyProperty TextTypeProperty = DependencyProperty.Register( "TextType", typeof(TextType), typeof(NumericUpDown), new PropertyMetadata(default(TextType))); - /// - /// 文本类型 - /// public TextType TextType { get => (TextType) GetValue(TextTypeProperty); @@ -354,24 +354,14 @@ namespace HandyControl.Controls public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.Register( "ShowClearButton", typeof(bool), typeof(NumericUpDown), new PropertyMetadata(default(bool))); - /// - /// 是否显示清除按钮 - /// public bool ShowClearButton { get => (bool) GetValue(ShowClearButtonProperty); set => SetValue(ShowClearButtonProperty, value); } - /// - /// 数据验证委托 - /// public Func> VerifyFunc { get; set; } - /// - /// 验证数据 - /// - /// public virtual bool VerifyData() { OperationResult result; diff --git a/HandyControl/Controls/Input/TextBox.cs b/HandyControl/Controls/Input/TextBox.cs index 96fd0046..e3224c98 100644 --- a/HandyControl/Controls/Input/TextBox.cs +++ b/HandyControl/Controls/Input/TextBox.cs @@ -1,8 +1,115 @@ -// ReSharper disable once CheckNamespace +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using HandyControl.Data; +using HandyControl.Interactivity; +using HandyControl.Tools; + +// ReSharper disable once CheckNamespace namespace HandyControl.Controls { - public class TextBox : System.Windows.Controls.TextBox + public class TextBox : System.Windows.Controls.TextBox, IDataInput { - + public TextBox() + { + CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) => + { + ClearValue(TextProperty); + Text = string.Empty; + })); + } + + protected override void OnTextChanged(TextChangedEventArgs e) + { + base.OnTextChanged(e); + VerifyData(); + } + + public Func> VerifyFunc { get; set; } + + /// + /// 数据是否错误 + /// + public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register( + "IsError", typeof(bool), typeof(TextBox), new PropertyMetadata(default(bool))); + + public bool IsError + { + get => (bool) GetValue(IsErrorProperty); + set => SetValue(IsErrorProperty, value); + } + + /// + /// 错误提示 + /// + public static readonly DependencyProperty ErrorStrProperty = DependencyProperty.Register( + "ErrorStr", typeof(string), typeof(TextBox), 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(TextBox), 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(TextBox), new PropertyMetadata(default(bool))); + + public bool ShowClearButton + { + get => (bool) GetValue(ShowClearButtonProperty); + set => SetValue(ShowClearButtonProperty, value); + } + + public virtual bool VerifyData() + { + OperationResult result; + + if (VerifyFunc != null) + { + result = VerifyFunc.Invoke(Text); + } + else + { + if (!string.IsNullOrEmpty(Text)) + { + if (TextType != TextType.Common) + { + result = Text.IsKindOf(TextType) ? OperationResult.Success() : OperationResult.Failed(Properties.Langs.Lang.FormatError); + } + else + { + 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; + } } } \ No newline at end of file diff --git a/HandyControl/Controls/Picker/TimePicker.cs b/HandyControl/Controls/Input/TimePicker.cs similarity index 100% rename from HandyControl/Controls/Picker/TimePicker.cs rename to HandyControl/Controls/Input/TimePicker.cs diff --git a/HandyControl/Controls/Loading/LoadingLine.cs b/HandyControl/Controls/Loading/LoadingLine.cs index 9f98a7c6..6edbf977 100644 --- a/HandyControl/Controls/Loading/LoadingLine.cs +++ b/HandyControl/Controls/Loading/LoadingLine.cs @@ -7,9 +7,6 @@ using System.Windows.Shapes; // ReSharper disable once CheckNamespace namespace HandyControl.Controls { - /// - /// LoadingLine.xaml 的交互逻辑 - /// public class LoadingLine : LoadingBase { private const double MoveLength = 80; diff --git a/HandyControl/Controls/Steps/StepBar.cs b/HandyControl/Controls/Steps/StepBar.cs index 57696860..15654df5 100644 --- a/HandyControl/Controls/Steps/StepBar.cs +++ b/HandyControl/Controls/Steps/StepBar.cs @@ -67,12 +67,18 @@ namespace HandyControl.Controls public override void OnApplyTemplate() { + _panelMain?.Children.Clear(); base.OnApplyTemplate(); _progressBarBack = GetTemplateChild(ElementProgressBarBack) as ProgressBar; _panelMain = GetTemplateChild(ElementUniformGridMain) as Panel; CheckNull(); + + if (_isLoaded) + { + UpdateItems(Items); + } } private void CheckNull() diff --git a/HandyControl/Controls/TimeBar/TimeBar.cs b/HandyControl/Controls/TimeBar/TimeBar.cs index 83ba1e63..ffce6df7 100644 --- a/HandyControl/Controls/TimeBar/TimeBar.cs +++ b/HandyControl/Controls/TimeBar/TimeBar.cs @@ -150,12 +150,14 @@ namespace HandyControl.Controls /// private double _totalOffsetX; + private readonly bool _isLoaded; + public TimeBar() { _starTime = DateTime.Now; SelectedTime = new DateTime(_starTime.Year, _starTime.Month, _starTime.Day, 0, 0, 0); _starTime = SelectedTime; - + _isLoaded = true; SizeChanged += TimeBar_OnSizeChanged; MouseMove += TimeBar_OnMouseMove; } @@ -188,6 +190,10 @@ namespace HandyControl.Controls var collection = Interaction.GetBehaviors(_borderTop); collection.Add(behavior); + if (_isLoaded) + { + TimeBar_OnSizeChanged(null, null); + } _textBlockSelected.Text = SelectedTime.ToString(TimeFormat); } diff --git a/HandyControl/HandyControl.csproj b/HandyControl/HandyControl.csproj index 98311e31..465401dc 100644 --- a/HandyControl/HandyControl.csproj +++ b/HandyControl/HandyControl.csproj @@ -51,14 +51,14 @@ - + - + - + @@ -116,7 +116,7 @@ - + diff --git a/HandyControl/Themes/Styles/Slider.xaml b/HandyControl/Themes/Styles/Slider.xaml index 37ab1339..c4fd9975 100644 --- a/HandyControl/Themes/Styles/Slider.xaml +++ b/HandyControl/Themes/Styles/Slider.xaml @@ -452,7 +452,7 @@ - + @@ -468,7 +468,7 @@ - + @@ -646,7 +646,7 @@ - + @@ -701,7 +701,7 @@ - + @@ -722,11 +722,9 @@ - - diff --git a/HandyControl/Themes/Styles/TextBox.xaml b/HandyControl/Themes/Styles/TextBox.xaml index 13ddbdd4..0692ea55 100644 --- a/HandyControl/Themes/Styles/TextBox.xaml +++ b/HandyControl/Themes/Styles/TextBox.xaml @@ -1,9 +1,12 @@  + xmlns:system="clr-namespace:System;assembly=mscorlib" + xmlns:interactivity="clr-namespace:HandyControl.Interactivity"> + + @@ -80,55 +83,121 @@ + + + + + + + + - + + - - - + + + + + + + + +