SearchBar.axaml.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 SearchBar()
  31. {
  32. InitializeComponent();
  33. }
  34. private void TextBox_TextChanged(object? sender, TextChangedEventArgs e)
  35. {
  36. if (Command is not null && Command.CanExecute(null))
  37. {
  38. Command.Execute(null);
  39. }
  40. }
  41. }