1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| namespace MyControls { public static class HighlightTermBehavior { public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached( "Text", typeof(string), typeof(HighlightTermBehavior), new FrameworkPropertyMetadata("", OnTextChanged));
public static string GetText(FrameworkElement frameworkElement) => (string)frameworkElement.GetValue(TextProperty); public static void SetText(FrameworkElement frameworkElement, string value) => frameworkElement.SetValue(TextProperty, value);
public static readonly DependencyProperty TermToBeHighlightedProperty = DependencyProperty.RegisterAttached( "TermToBeHighlighted", typeof(string), typeof(HighlightTermBehavior), new FrameworkPropertyMetadata("", OnTextChanged));
public static string GetTermToBeHighlighted(FrameworkElement frameworkElement) { return (string)frameworkElement.GetValue(TermToBeHighlightedProperty); }
public static void SetTermToBeHighlighted(FrameworkElement frameworkElement, string value) { frameworkElement.SetValue(TermToBeHighlightedProperty, value); }
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is TextBlock textBlock) SetTextBlockTextAndHighlightTerm(textBlock, GetText(textBlock), GetTermToBeHighlighted(textBlock)); }
private static void SetTextBlockTextAndHighlightTerm(TextBlock textBlock, string text, string termToBeHighlighted) { textBlock.Text = string.Empty;
if (string.IsNullOrEmpty(text)) return; text = string.Format(text, termToBeHighlighted.Split('|'));
var textParts = SplitTextIntoTermAndNotTermParts(text, termToBeHighlighted);
foreach (var textPart in textParts) AddPartToTextBlockAndHighlightIfNecessary(textBlock, termToBeHighlighted, textPart); }
private static void AddPartToTextBlockAndHighlightIfNecessary(TextBlock textBlock, string termToBeHighlighted, string textPart) { if (termToBeHighlighted.Contains(textPart)) AddHighlightedPartToTextBlock(textBlock, textPart); else AddPartToTextBlock(textBlock, textPart); }
private static void AddPartToTextBlock(TextBlock textBlock, string part) { textBlock.Inlines.Add(new Run { Text = part, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0)) }); }
private static void AddHighlightedPartToTextBlock(TextBlock textBlock, string part) { textBlock.Inlines.Add(new Run { Text = part, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Color.FromRgb(0xFF, 0, 0)) }); }
public static List<string> SplitTextIntoTermAndNotTermParts(string text, string term) { if (string.IsNullOrEmpty(text)) return new List<string>() { string.Empty };
return Regex.Split(text, $@"({term})") .Where(p => p != string.Empty) .ToList(); } } }
|