diff --git a/HandyControl/Controls/Input/SearchBar.cs b/HandyControl/Controls/Input/SearchBar.cs index 5657ae1f..d34a3557 100644 --- a/HandyControl/Controls/Input/SearchBar.cs +++ b/HandyControl/Controls/Input/SearchBar.cs @@ -1,31 +1,21 @@ using System; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; using System.Windows.Input; using HandyControl.Data; using HandyControl.Interactivity; -using HandyControl.Tools; namespace HandyControl.Controls { - [TemplatePart(Name = ElementTextBox, Type = typeof(WatermarkTextBox))] - public class SearchBar : Control, IDataInput + public class SearchBar : TextBox { - private const string ElementTextBox = "PART_TextBox"; - - private WatermarkTextBox _textBox; - - public Func> VerifyFunc { get; set; } - public SearchBar() { - CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) => _textBox.Text = string.Empty)); CommandBindings.Add(new CommandBinding(ControlCommands.Search, (s, e) => OnSearchStarted())); } public static readonly RoutedEvent SearchStartedEvent = - EventManager.RegisterRoutedEvent("SearchStarted", RoutingStrategy.Direct, + EventManager.RegisterRoutedEvent("SearchStarted", RoutingStrategy.Bubble, typeof(EventHandler>), typeof(SearchBar)); public event EventHandler> SearchStarted @@ -34,41 +24,20 @@ namespace HandyControl.Controls remove => RemoveHandler(SearchStartedEvent, value); } - public override void OnApplyTemplate() + protected override void OnKeyDown(KeyEventArgs e) { - if (_textBox != null) - { - _textBox.TextChanged -= TextBox_TextChanged; - _textBox.KeyDown -= TextBox_KeyDown; - } + base.OnKeyDown(e); - base.OnApplyTemplate(); - - _textBox = GetTemplateChild(ElementTextBox) as WatermarkTextBox; - if (_textBox != null) - { - _textBox.Text = Text; - SetBinding(TextProperty, new Binding(TextProperty.Name) - { - Source = _textBox, - Mode = BindingMode.TwoWay, - UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged - }); - _textBox.TextChanged += TextBox_TextChanged; - _textBox.KeyDown += TextBox_KeyDown; - } - } - - private void TextBox_KeyDown(object sender, KeyEventArgs e) - { if (e.Key == Key.Enter) { OnSearchStarted(); } } - private void TextBox_TextChanged(object sender, TextChangedEventArgs e) + protected override void OnTextChanged(TextChangedEventArgs e) { + base.OnTextChanged(e); + if (IsRealTime) { OnSearchStarted(); @@ -84,63 +53,6 @@ namespace HandyControl.Controls }); } - public static readonly DependencyProperty TextProperty = DependencyProperty.Register( - "Text", typeof(string), typeof(SearchBar), new PropertyMetadata(default(string))); - - public string Text - { - get => (string)GetValue(TextProperty); - set => SetValue(TextProperty, value); - } - - /// - /// 数据是否错误 - /// - public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register( - "IsError", typeof(bool), typeof(SearchBar), 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(SearchBar), 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(SearchBar), 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(SearchBar), new PropertyMetadata(ValueBoxes.FalseBox)); - - public bool ShowClearButton - { - get => (bool)GetValue(ShowClearButtonProperty); - set => SetValue(ShowClearButtonProperty, value); - } - /// /// 是否实时搜索 /// @@ -155,41 +67,5 @@ namespace HandyControl.Controls get => (bool)GetValue(IsRealTimeProperty); set => SetValue(IsRealTimeProperty, 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; - } } } diff --git a/HandyControl/Themes/Styles/Base/SearchBarBaseStyle.xaml b/HandyControl/Themes/Styles/Base/SearchBarBaseStyle.xaml index 7285a243..c7b37f41 100644 --- a/HandyControl/Themes/Styles/Base/SearchBarBaseStyle.xaml +++ b/HandyControl/Themes/Styles/Base/SearchBarBaseStyle.xaml @@ -5,20 +5,10 @@ xmlns:interactivity="clr-namespace:HandyControl.Interactivity"> - - - + -