TextBlockHighlighter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Documents;
  5. using System.Windows.Media;
  6. namespace MECF.Framework.UI.Client.ClientBase.AttachedProperties
  7. {
  8. public static class TextBlockHighlighter
  9. {
  10. public static string GetSelection(DependencyObject obj)
  11. {
  12. return (string)obj.GetValue(SelectionProperty);
  13. }
  14. public static void SetSelection(DependencyObject obj, string value)
  15. {
  16. obj.SetValue(SelectionProperty, value);
  17. }
  18. public static readonly DependencyProperty SelectionProperty =
  19. DependencyProperty.RegisterAttached("Selection", typeof(string), typeof(TextBlockHighlighter),
  20. new PropertyMetadata(new PropertyChangedCallback(SelectText)));
  21. private static void SelectText(DependencyObject d, DependencyPropertyChangedEventArgs e)
  22. {
  23. if (d == null)
  24. return;
  25. if (!(d is TextBlock txtBlock))
  26. throw new InvalidOperationException("Only valid for TextBlock");
  27. var text = txtBlock.Text;
  28. if (string.IsNullOrEmpty(text))
  29. return;
  30. var highlightText = (string)txtBlock.GetValue(SelectionProperty);
  31. if (string.IsNullOrEmpty(highlightText))
  32. {
  33. txtBlock.Inlines.Clear();
  34. txtBlock.Inlines.Add(text);
  35. return;
  36. }
  37. var index = text.IndexOf(highlightText, StringComparison.CurrentCultureIgnoreCase);
  38. if (index < 0)
  39. return;
  40. var selectionColor = (Brush)txtBlock.GetValue(HighlightColorProperty);
  41. var foreColor = (Brush)txtBlock.GetValue(ForeColorProperty);
  42. txtBlock.Inlines.Clear();
  43. while (true)
  44. {
  45. txtBlock.Inlines.AddRange(new Inline[]
  46. {
  47. new Run(text.Substring(0, index)),
  48. new Run(text.Substring(index, highlightText.Length))
  49. {
  50. Background = selectionColor,
  51. Foreground = foreColor
  52. }
  53. });
  54. text = text.Substring(index + highlightText.Length);
  55. index = text.IndexOf(highlightText, StringComparison.CurrentCultureIgnoreCase);
  56. if (index < 0)
  57. {
  58. txtBlock.Inlines.Add(new Run(text));
  59. break;
  60. }
  61. }
  62. }
  63. public static Brush GetHighlightColor(DependencyObject obj)
  64. {
  65. return (Brush)obj.GetValue(HighlightColorProperty);
  66. }
  67. public static void SetHighlightColor(DependencyObject obj, Brush value)
  68. {
  69. obj.SetValue(HighlightColorProperty, value);
  70. }
  71. public static readonly DependencyProperty HighlightColorProperty =
  72. DependencyProperty.RegisterAttached("HighlightColor", typeof(Brush), typeof(TextBlockHighlighter),
  73. new PropertyMetadata(Brushes.Yellow, new PropertyChangedCallback(SelectText)));
  74. public static Brush GetForeColor(DependencyObject obj)
  75. {
  76. return (Brush)obj.GetValue(ForeColorProperty);
  77. }
  78. public static void SetForeColor(DependencyObject obj, Brush value)
  79. {
  80. obj.SetValue(ForeColorProperty, value);
  81. }
  82. public static readonly DependencyProperty ForeColorProperty =
  83. DependencyProperty.RegisterAttached("ForeColor", typeof(Brush), typeof(TextBlockHighlighter),
  84. new PropertyMetadata(Brushes.Black, new PropertyChangedCallback(SelectText)));
  85. }
  86. }