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 @@ - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +