andrew4582

HighlightTextBlock WPF

Jul 10th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Documents;
  5. using System.Windows.Media;
  6.  
  7. namespace Controls
  8. {
  9.     public class HighlightTextBlock : TextBlock
  10.     {
  11.         #region Properties
  12.  
  13.         public new string Text
  14.         {
  15.             get { return (string)GetValue(TextProperty); }
  16.             set { SetValue(TextProperty, value); }
  17.         }
  18.  
  19.         public new static readonly DependencyProperty TextProperty =
  20.             DependencyProperty.Register("Text", typeof(string),
  21.             typeof(HighlightTextBlock), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender,
  22.                 UpdateHighlighting));
  23.  
  24.         public string HighlightPhrase
  25.         {
  26.             get { return (string)GetValue(HighlightPhraseProperty); }
  27.             set { SetValue(HighlightPhraseProperty, value); }
  28.         }
  29.  
  30.         public static readonly DependencyProperty HighlightPhraseProperty =
  31.             DependencyProperty.Register("HighlightPhrase", typeof(string),
  32.             typeof(HighlightTextBlock), new FrameworkPropertyMetadata(String.Empty, FrameworkPropertyMetadataOptions.AffectsRender,
  33.                 UpdateHighlighting));
  34.  
  35.         public Brush HighlightBrush
  36.         {
  37.             get { return (Brush)GetValue(HighlightBrushProperty); }
  38.             set { SetValue(HighlightBrushProperty, value); }
  39.         }
  40.  
  41.         public static readonly DependencyProperty HighlightBrushProperty =
  42.             DependencyProperty.Register("HighlightBrush", typeof(Brush),
  43.             typeof(HighlightTextBlock), new FrameworkPropertyMetadata(Brushes.Yellow, FrameworkPropertyMetadataOptions.AffectsRender,
  44.                 UpdateHighlighting));
  45.  
  46.         public bool IsCaseSensitive
  47.         {
  48.             get { return (bool)GetValue(IsCaseSensitiveProperty); }
  49.             set { SetValue(IsCaseSensitiveProperty, value); }
  50.         }
  51.  
  52.         public static readonly DependencyProperty IsCaseSensitiveProperty =
  53.             DependencyProperty.Register("IsCaseSensitive", typeof(bool),
  54.             typeof(HighlightTextBlock), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender,
  55.                 UpdateHighlighting));
  56.  
  57.         private static void UpdateHighlighting(DependencyObject d, DependencyPropertyChangedEventArgs e)
  58.         {
  59.             ApplyHighlight(d as HighlightTextBlock);
  60.         }
  61.  
  62.         #endregion
  63.  
  64.         #region Members
  65.  
  66.         private static void ApplyHighlight(HighlightTextBlock tb)
  67.         {
  68.             string highlightPhrase = tb.HighlightPhrase;
  69.             string text = tb.Text;
  70.  
  71.             if (String.IsNullOrEmpty(highlightPhrase))
  72.             {
  73.                 tb.Inlines.Clear();
  74.  
  75.                 tb.Inlines.Add(text);
  76.             }
  77.  
  78.             else
  79.             {
  80.                 highlightPhrase = highlightPhrase.Trim();
  81.                  
  82.                 int index = text.IndexOf(highlightPhrase, (tb.IsCaseSensitive) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
  83.  
  84.                 tb.Inlines.Clear();
  85.  
  86.                 if (index < 0) //if highlightPhrase doesn't exist in text
  87.                     tb.Inlines.Add(text); //add text, with no background highlighting, to tb.Inlines
  88.  
  89.                 else
  90.                 {
  91.                     if (index > 0) //if highlightPhrase occurs after start of text
  92.                         tb.Inlines.Add(text.Substring(0, index)); //add the text that exists before highlightPhrase, with no background highlighting, to tb.Inlines
  93.  
  94.                     //add the highlightPhrase, using substring to get the casing as it appears in text, with a background, to tb.Inlines
  95.                     tb.Inlines.Add(new Run(text.Substring(index, highlightPhrase.Length))
  96.                     {
  97.                         Background = tb.HighlightBrush
  98.                     });
  99.  
  100.                     index += highlightPhrase.Length; //move index to the end of the matched highlightPhrase
  101.  
  102.                     if (index < text.Length) //if the end of the matched highlightPhrase occurs before the end of text
  103.                         tb.Inlines.Add(text.Substring(index)); //add the text that exists after highlightPhrase, with no background highlighting, to tb.Inlines
  104.                 }
  105.             }
  106.         }
  107.  
  108.         #endregion
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment