TextSearch.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Avalonia.Controls.Utils;
  2. using Avalonia.Data;
  3. using Avalonia.Interactivity;
  4. namespace Avalonia.Controls.Primitives
  5. {
  6. /// <summary>
  7. /// Allows to customize text searching in <see cref="SelectingItemsControl"/>.
  8. /// </summary>
  9. public static class TextSearch
  10. {
  11. /// <summary>
  12. /// Defines the Text attached property.
  13. /// This text will be considered during text search in <see cref="SelectingItemsControl"/> (such as <see cref="ComboBox"/>).
  14. /// This property is usually applied to an item container directly.
  15. /// </summary>
  16. public static readonly AttachedProperty<string?> TextProperty
  17. = AvaloniaProperty.RegisterAttached<Interactive, string?>("Text", typeof(TextSearch));
  18. /// <summary>
  19. /// Defines the TextBinding attached property.
  20. /// The binding will be applied to each item during text search in <see cref="SelectingItemsControl"/> (such as <see cref="ComboBox"/>).
  21. /// </summary>
  22. public static readonly AttachedProperty<BindingBase?> TextBindingProperty
  23. = AvaloniaProperty.RegisterAttached<Interactive, BindingBase?>("TextBinding", typeof(TextSearch));
  24. // TODO12: Control should be Interactive to match the property definition.
  25. /// <summary>
  26. /// Sets the value of the <see cref="TextProperty"/> attached property to a given <see cref="Control"/>.
  27. /// </summary>
  28. /// <param name="control">The control.</param>
  29. /// <param name="text">The search text to set.</param>
  30. public static void SetText(Control control, string? text)
  31. => control.SetValue(TextProperty, text);
  32. // TODO12: Control should be Interactive to match the property definition.
  33. /// <summary>
  34. /// Gets the value of the <see cref="TextProperty"/> attached property from a given <see cref="Control"/>.
  35. /// </summary>
  36. /// <param name="control">The control.</param>
  37. /// <returns>The search text.</returns>
  38. public static string? GetText(Control control)
  39. => control.GetValue(TextProperty);
  40. /// <summary>
  41. /// Sets the value of the <see cref="TextBindingProperty"/> attached property to a given <see cref="Interactive"/>.
  42. /// </summary>
  43. /// <param name="interactive">The interactive element.</param>
  44. /// <param name="value">The search text binding to set.</param>
  45. public static void SetTextBinding(Interactive interactive, BindingBase? value)
  46. => interactive.SetValue(TextBindingProperty, value);
  47. /// <summary>
  48. /// Gets the value of the <see cref="TextBindingProperty"/> attached property from a given <see cref="Interactive"/>.
  49. /// </summary>
  50. /// <param name="interactive">The interactive element.</param>
  51. /// <returns>The search text binding.</returns>
  52. [AssignBinding]
  53. public static BindingBase? GetTextBinding(Interactive interactive)
  54. => interactive.GetValue(TextBindingProperty);
  55. /// <summary>
  56. /// <para>Gets the effective text of a given item.</para>
  57. /// <para>
  58. /// This method uses the first non-empty text from the following list:
  59. /// <list>
  60. /// <item><see cref="TextSearch.TextProperty"/> (if the item is a control)</item>
  61. /// <item><see cref="TextSearch.TextBindingProperty"/></item>
  62. /// <item><see cref="ItemsControl.DisplayMemberBinding"/></item>
  63. /// <item><see cref="IContentControl.Content"/>.<see cref="object.ToString"/> (if the item is a <see cref="IContentControl"/>)</item>
  64. /// <item><see cref="object.ToString"/></item>
  65. /// </list>
  66. /// </para>
  67. /// </summary>
  68. /// <param name="item">The item.</param>
  69. /// <param name="textBindingEvaluator">A <see cref="BindingEvaluator{T}"/> used to get the item's text from a binding.</param>
  70. /// <returns>The item's text.</returns>
  71. internal static string GetEffectiveText(object? item, BindingEvaluator<string?>? textBindingEvaluator)
  72. {
  73. if (item is null)
  74. return string.Empty;
  75. string? text;
  76. if (item is Interactive interactive)
  77. {
  78. text = interactive.GetValue(TextProperty);
  79. if (!string.IsNullOrEmpty(text))
  80. return text;
  81. }
  82. if (textBindingEvaluator is not null)
  83. {
  84. text = textBindingEvaluator.Evaluate(item);
  85. if (!string.IsNullOrEmpty(text))
  86. return text;
  87. }
  88. if (item is IContentControl contentControl)
  89. return contentControl.Content?.ToString() ?? string.Empty;
  90. return item.ToString() ?? string.Empty;
  91. }
  92. }
  93. }