SearchBar.axaml.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Data;
  4. using Avalonia.Markup.Xaml;
  5. using System.Windows.Input;
  6. namespace InABox.Avalonia.Components;
  7. public partial class SearchBar : UserControl
  8. {
  9. public static readonly StyledProperty<string> PlaceholderTextProperty =
  10. AvaloniaProperty.Register<SearchBar, string>(nameof(PlaceholderText), "Search...");
  11. public string PlaceholderText
  12. {
  13. get => GetValue(PlaceholderTextProperty);
  14. set => SetValue(PlaceholderTextProperty, value);
  15. }
  16. public static readonly StyledProperty<ICommand?> CommandProperty =
  17. AvaloniaProperty.Register<SearchBar, ICommand?>(nameof(Command), null);
  18. public ICommand? Command
  19. {
  20. get => GetValue(CommandProperty);
  21. set => SetValue(CommandProperty, value);
  22. }
  23. public static readonly StyledProperty<string> TextProperty =
  24. AvaloniaProperty.Register<SearchBar, string>(nameof(Text), "", defaultBindingMode: BindingMode.TwoWay);
  25. public string Text
  26. {
  27. get => GetValue(TextProperty);
  28. set => SetValue(TextProperty, value);
  29. }
  30. public static readonly StyledProperty<bool> SearchOnChangedProperty =
  31. AvaloniaProperty.Register<SearchBar, bool>(nameof(SearchOnChanged), true);
  32. public bool SearchOnChanged
  33. {
  34. get => GetValue(SearchOnChangedProperty);
  35. set => SetValue(SearchOnChangedProperty, value);
  36. }
  37. public SearchBar()
  38. {
  39. InitializeComponent();
  40. }
  41. private void TextBox_TextChanged(object? sender, TextChangedEventArgs e)
  42. {
  43. if (SearchOnChanged && Command is not null && Command.CanExecute(null))
  44. {
  45. Command.Execute(null);
  46. }
  47. }
  48. }