SearchTextBlock.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Documents;
  10. using System.Windows.Media;
  11. using System.Windows.Threading;
  12. namespace FWindSoft.Wpf.Controls
  13. {
  14. /// <summary>
  15. /// 搜索使用textBlock
  16. /// </summary>
  17. public class SearchTextBlock:TextBlock
  18. {
  19. public static readonly DependencyProperty SearchBackgroundProperty = DependencyProperty.Register("SearchBackground",
  20. typeof(Brush),
  21. typeof(SearchTextBlock), new PropertyMetadata(Brushes.Yellow, new PropertyChangedCallback(PropertyChangedCallback)));
  22. public static readonly DependencyProperty SearchForegroundProperty = DependencyProperty.Register("SearchForeground",
  23. typeof(Brush),
  24. typeof(SearchTextBlock), new PropertyMetadata(Brushes.Black, new PropertyChangedCallback(PropertyChangedCallback)));
  25. #region 关键字附加属性
  26. public static readonly DependencyProperty SearchKeyProperty =
  27. DependencyProperty.RegisterAttached("SearchKey", typeof(string), typeof(SearchTextBlock), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits ,new PropertyChangedCallback(PropertyChangedCallback)));
  28. public static string GetSearchKey(DependencyObject dp)
  29. {
  30. return dp.GetValue(SearchKeyProperty) as string;
  31. }
  32. public static void SetSearchKey(DependencyObject dp, string value)
  33. {
  34. dp.SetValue(SearchKeyProperty, value);
  35. }
  36. #endregion
  37. static SearchTextBlock()
  38. {
  39. try
  40. {
  41. TextProperty.OverrideMetadata(typeof(SearchTextBlock), new FrameworkPropertyMetadata((object)string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(PropertyChangedCallback)));
  42. }
  43. catch (Exception e)
  44. {
  45. }
  46. }
  47. public Brush SearchBackground
  48. {
  49. set { SetValue(SearchBackgroundProperty, value); }
  50. get { return (Brush)GetValue(SearchBackgroundProperty); }
  51. }
  52. public Brush SearchForeground
  53. {
  54. set { SetValue(SearchForegroundProperty, value); }
  55. get { return (Brush)GetValue(SearchForegroundProperty); }
  56. }
  57. public static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  58. {
  59. try
  60. {
  61. if (!(d is SearchTextBlock ))
  62. {
  63. return;
  64. }
  65. var block = (SearchTextBlock) d;
  66. block.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(block.RefreshShow));
  67. }
  68. catch (Exception ex)
  69. {
  70. }
  71. }
  72. #region 内部编辑
  73. //内部编辑模块和BeginInvoke(DispatcherPriority.Background基本可替代?,有时间再验证
  74. #endregion
  75. public void RefreshShow()
  76. {
  77. try
  78. {
  79. var stringKey = GetSearchKey(this);
  80. if (string.IsNullOrEmpty(stringKey))
  81. return;
  82. string newStr = this.Text ?? string.Empty;
  83. this.Inlines.Clear();
  84. int index = -1;
  85. while ((index = newStr.IndexOf(stringKey)) > -1)
  86. {
  87. if (index > 0)
  88. {
  89. this.Inlines.Add(new Run(newStr.Substring(0, index)));
  90. }
  91. this.Inlines.Add(new Run(newStr.Substring(index, stringKey.Length)) { Background = this.SearchBackground, Foreground = this.SearchForeground });
  92. newStr = newStr.Substring(index + stringKey.Length);
  93. }
  94. this.Inlines.Add(new Run(newStr));
  95. }
  96. catch (Exception ex)
  97. {
  98. Debug.Write(ex.Message);
  99. }
  100. }
  101. }
  102. }