From aece5e3a44dd3ec09c2016b2a85bac24b969336e Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sat, 17 Nov 2018 16:58:11 +0800 Subject: [PATCH] enhance the function of ComboBox --- HandyControl/Controls/Input/ComboBox.cs | 112 ++++ HandyControl/Controls/Input/NumericUpDown.cs | 7 +- HandyControl/Controls/Input/TextBox.cs | 2 +- HandyControl/HandyControl.csproj | 1 + .../Themes/Styles/Base/ComboBoxBaseStyle.xaml | 521 ++++++++++++++++-- .../Themes/Styles/Base/TextBoxBaseStyle.xaml | 204 ++++++- HandyControl/Themes/Styles/ComboBox.xaml | 5 +- HandyControl/Themes/Styles/TextBox.xaml | 204 +------ HandyControlDemo/Data/MessageToken.cs | 4 +- HandyControlDemo/Data/MessageToken.tt | 5 +- HandyControlDemo/HandyControlDemo.csproj | 13 +- .../UserControl/Controls/ComboBoxDemoCtl.xaml | 27 + .../ComboBoxDemoCtl.xaml.cs | 3 +- .../UserControl/Main/LeftMainContent.xaml | 7 +- ...DemoCtl.xaml => NaiveComboBoxDemoCtl.xaml} | 2 +- .../Styles/NaiveComboBoxDemoCtl.xaml.cs | 10 + 16 files changed, 868 insertions(+), 259 deletions(-) create mode 100644 HandyControl/Controls/Input/ComboBox.cs create mode 100644 HandyControlDemo/UserControl/Controls/ComboBoxDemoCtl.xaml rename HandyControlDemo/UserControl/{Styles => Controls}/ComboBoxDemoCtl.xaml.cs (63%) rename HandyControlDemo/UserControl/Styles/{ComboBoxDemoCtl.xaml => NaiveComboBoxDemoCtl.xaml} (97%) create mode 100644 HandyControlDemo/UserControl/Styles/NaiveComboBoxDemoCtl.xaml.cs diff --git a/HandyControl/Controls/Input/ComboBox.cs b/HandyControl/Controls/Input/ComboBox.cs new file mode 100644 index 00000000..3eb05716 --- /dev/null +++ b/HandyControl/Controls/Input/ComboBox.cs @@ -0,0 +1,112 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using HandyControl.Data; +using HandyControl.Interactivity; + +namespace HandyControl.Controls +{ + /// + public class ComboBox : System.Windows.Controls.ComboBox, IDataInput + { + public ComboBox() + { + CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) => + { + ClearValue(SelectedValueProperty); + ClearValue(SelectedItemProperty); + ClearValue(SelectedIndexProperty); + ClearValue(TextProperty); + })); + } + + protected override void OnSelectionChanged(SelectionChangedEventArgs e) + { + base.OnSelectionChanged(e); + VerifyData(); + } + + public Func> VerifyFunc { get; set; } + + /// + /// 数据是否错误 + /// + public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register( + "IsError", typeof(bool), typeof(ComboBox), 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(ComboBox), new PropertyMetadata(default(string))); + + public string ErrorStr + { + get => (string) GetValue(ErrorStrProperty); + set => SetValue(ErrorStrProperty, value); + } + + public static readonly DependencyPropertyKey TextTypePropertyKey = + DependencyProperty.RegisterReadOnly("TextType", typeof(TextType), typeof(ComboBox), + new PropertyMetadata(default(TextType))); + + /// + /// 文本类型 + /// + public static readonly DependencyProperty TextTypeProperty = TextTypePropertyKey.DependencyProperty; + + public TextType TextType + { + get => (TextType) GetValue(TextTypeProperty); + set => SetValue(TextTypeProperty, value); + } + + /// + /// 是否显示清除按钮 + /// + public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.Register( + "ShowClearButton", typeof(bool), typeof(ComboBox), 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)) + { + 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/Input/NumericUpDown.cs b/HandyControl/Controls/Input/NumericUpDown.cs index 78b902e0..f6b70ca5 100644 --- a/HandyControl/Controls/Input/NumericUpDown.cs +++ b/HandyControl/Controls/Input/NumericUpDown.cs @@ -336,11 +336,14 @@ namespace HandyControl.Controls set => SetValue(ErrorStrProperty, value); } + public static readonly DependencyPropertyKey TextTypePropertyKey = + DependencyProperty.RegisterReadOnly("TextType", typeof(TextType), typeof(NumericUpDown), + new PropertyMetadata(default(TextType))); + /// /// 文本类型 /// - public static readonly DependencyProperty TextTypeProperty = DependencyProperty.Register( - "TextType", typeof(TextType), typeof(NumericUpDown), new PropertyMetadata(default(TextType))); + public static readonly DependencyProperty TextTypeProperty = TextTypePropertyKey.DependencyProperty; public TextType TextType { diff --git a/HandyControl/Controls/Input/TextBox.cs b/HandyControl/Controls/Input/TextBox.cs index 815e6ade..2216919c 100644 --- a/HandyControl/Controls/Input/TextBox.cs +++ b/HandyControl/Controls/Input/TextBox.cs @@ -9,6 +9,7 @@ using HandyControl.Tools; // ReSharper disable once CheckNamespace namespace HandyControl.Controls { + /// public class TextBox : System.Windows.Controls.TextBox, IDataInput { public TextBox() @@ -16,7 +17,6 @@ namespace HandyControl.Controls CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) => { ClearValue(TextProperty); - Text = string.Empty; })); } diff --git a/HandyControl/HandyControl.csproj b/HandyControl/HandyControl.csproj index 772869a8..dbdfa326 100644 --- a/HandyControl/HandyControl.csproj +++ b/HandyControl/HandyControl.csproj @@ -52,6 +52,7 @@ + diff --git a/HandyControl/Themes/Styles/Base/ComboBoxBaseStyle.xaml b/HandyControl/Themes/Styles/Base/ComboBoxBaseStyle.xaml index 472fb1e8..fd89b9ec 100644 --- a/HandyControl/Themes/Styles/Base/ComboBoxBaseStyle.xaml +++ b/HandyControl/Themes/Styles/Base/ComboBoxBaseStyle.xaml @@ -1,14 +1,17 @@  + xmlns:system="clr-namespace:System;assembly=mscorlib" + xmlns:interactivity="clr-namespace:HandyControl.Interactivity"> + + + + @@ -101,19 +106,13 @@ - - - - - - - + @@ -165,19 +164,13 @@ - - - - - - - + @@ -226,19 +219,13 @@ - - - - - - - + + @@ -309,19 +297,13 @@ - - - - - - - + @@ -383,19 +365,13 @@ - - - - - - - + @@ -454,19 +430,13 @@ - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +