SearchBar.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using HandyControl.Data;
  6. using HandyControl.Interactivity;
  7. namespace HandyControl.Controls;
  8. public class SearchBar : TextBox, ICommandSource
  9. {
  10. public SearchBar()
  11. {
  12. CommandBindings.Add(new CommandBinding(ControlCommands.Search, (s, e) => OnSearchStarted()));
  13. }
  14. public static readonly RoutedEvent SearchStartedEvent =
  15. EventManager.RegisterRoutedEvent("SearchStarted", RoutingStrategy.Bubble,
  16. typeof(EventHandler<FunctionEventArgs<string>>), typeof(SearchBar));
  17. public event EventHandler<FunctionEventArgs<string>> SearchStarted
  18. {
  19. add => AddHandler(SearchStartedEvent, value);
  20. remove => RemoveHandler(SearchStartedEvent, value);
  21. }
  22. protected override void OnKeyDown(KeyEventArgs e)
  23. {
  24. base.OnKeyDown(e);
  25. if (e.Key == Key.Enter)
  26. {
  27. OnSearchStarted();
  28. }
  29. }
  30. protected override void OnTextChanged(TextChangedEventArgs e)
  31. {
  32. base.OnTextChanged(e);
  33. if (IsRealTime)
  34. {
  35. OnSearchStarted();
  36. }
  37. }
  38. private void OnSearchStarted()
  39. {
  40. RaiseEvent(new FunctionEventArgs<string>(SearchStartedEvent, this)
  41. {
  42. Info = Text
  43. });
  44. switch (Command)
  45. {
  46. case null:
  47. return;
  48. case RoutedCommand command:
  49. command.Execute(CommandParameter, CommandTarget);
  50. break;
  51. default:
  52. Command.Execute(CommandParameter);
  53. break;
  54. }
  55. }
  56. /// <summary>
  57. /// 是否实时搜索
  58. /// </summary>
  59. public static readonly DependencyProperty IsRealTimeProperty = DependencyProperty.Register(
  60. nameof(IsRealTime), typeof(bool), typeof(SearchBar), new PropertyMetadata(ValueBoxes.FalseBox));
  61. /// <summary>
  62. /// 是否实时搜索
  63. /// </summary>
  64. public bool IsRealTime
  65. {
  66. get => (bool) GetValue(IsRealTimeProperty);
  67. set => SetValue(IsRealTimeProperty, ValueBoxes.BooleanBox(value));
  68. }
  69. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  70. nameof(Command), typeof(ICommand), typeof(SearchBar), new PropertyMetadata(default(ICommand), OnCommandChanged));
  71. private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  72. {
  73. var ctl = (SearchBar) d;
  74. if (e.OldValue is ICommand oldCommand)
  75. {
  76. oldCommand.CanExecuteChanged -= ctl.CanExecuteChanged;
  77. }
  78. if (e.NewValue is ICommand newCommand)
  79. {
  80. newCommand.CanExecuteChanged += ctl.CanExecuteChanged;
  81. }
  82. }
  83. public ICommand Command
  84. {
  85. get => (ICommand) GetValue(CommandProperty);
  86. set => SetValue(CommandProperty, value);
  87. }
  88. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
  89. nameof(CommandParameter), typeof(object), typeof(SearchBar), new PropertyMetadata(default(object)));
  90. public object CommandParameter
  91. {
  92. get => GetValue(CommandParameterProperty);
  93. set => SetValue(CommandParameterProperty, value);
  94. }
  95. public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register(
  96. nameof(CommandTarget), typeof(IInputElement), typeof(SearchBar), new PropertyMetadata(default(IInputElement)));
  97. public IInputElement CommandTarget
  98. {
  99. get => (IInputElement) GetValue(CommandTargetProperty);
  100. set => SetValue(CommandTargetProperty, value);
  101. }
  102. private void CanExecuteChanged(object sender, EventArgs e)
  103. {
  104. if (Command == null) return;
  105. IsEnabled = Command is RoutedCommand command
  106. ? command.CanExecute(CommandParameter, CommandTarget)
  107. : Command.CanExecute(CommandParameter);
  108. }
  109. }