11using System ;
2+ using System . ComponentModel ;
23using System . Text . RegularExpressions ;
34using System . Windows ;
45using System . Windows . Controls ;
@@ -11,6 +12,21 @@ public static class TextBlockHelper
1112 {
1213 #region Properties
1314
15+ #region Text
16+ public static string GetText ( TextBlock textBlock )
17+ {
18+ return ( string ) textBlock . GetValue ( TextProperty ) ;
19+ }
20+
21+ public static void SetText ( TextBlock textBlock , string value )
22+ {
23+ textBlock . SetValue ( TextProperty , value ) ;
24+ }
25+
26+ public static readonly DependencyProperty TextProperty =
27+ DependencyProperty . RegisterAttached ( "Text" , typeof ( string ) , typeof ( TextBlockHelper ) , new PropertyMetadata ( OnHighlightTextChanged ) ) ;
28+ #endregion
29+
1430 #region HighlightText
1531 public static string GetHighlightText ( TextBlock textBlock )
1632 {
@@ -97,7 +113,7 @@ private static void OnHighlightTextChanged(DependencyObject d, DependencyPropert
97113 {
98114 return ;
99115 }
100- var text = textBlock . Text ;
116+ var text = GetText ( textBlock ) ;
101117 var regex = GetHighlightRegex ( textBlock ) ;
102118 var highlightText = GetHighlightText ( textBlock ) ;
103119 var foreground = GetHighlightForeground ( textBlock ) ;
@@ -143,7 +159,7 @@ private static void OnHighlightTextChanged(DependencyObject d, DependencyPropert
143159 index = match . Index ;
144160 matchText = match . Value ;
145161
146- if ( string . IsNullOrEmpty ( matchText )
162+ if ( string . IsNullOrEmpty ( matchText )
147163 || rule == HighlightRule . FirstOnly )
148164 {
149165 textBlock . Inlines . Add ( new Run ( text ) ) ;
0 commit comments