Exemple #1
0
        private static void Swap(HighlightTextBlock tb)
        {
            Brush temp;

            temp                   = tb.Background;
            tb.Background          = tb.HighlightBackground;
            tb.HighlightBackground = temp;

            temp                   = tb.Foreground;
            tb.Foreground          = tb.HighlightForeground;
            tb.HighlightForeground = temp;

            temp = null;
        }
Exemple #2
0
        private static void MatchWholeWordPropertyChanged(DependencyObject sender,
                                                          DependencyPropertyChangedEventArgs e)
        {
            //getting the current instance of Textblock
            HighlightTextBlock tb = sender as HighlightTextBlock;

            if (!(bool)e.NewValue)
            {
                if (tb.Highlighted)
                {
                    Swap(tb);
                    tb.Highlighted = false;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Callback method when user search is changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void HighlightTextChanged(DependencyObject sender,
                                                 DependencyPropertyChangedEventArgs e)
        {
            //getting the current instance of Textblock
            HighlightTextBlock tb = sender as HighlightTextBlock;

            string completeText = tb.Text.Trim();
            string searchText   = e.NewValue.ToString().Trim();

            //Check if string case hase to be considered.
            if (!tb.MatchCase)
            {
                completeText = completeText.ToLower();
                searchText   = searchText.ToLower();
            }

            //Check if we need to compare the whole word.
            if (tb.MatchWholeWord)
            {
                if (completeText != string.Empty)
                {
                    //when the condition is true,
                    //set the highlight color specified by customer.
                    if (completeText == searchText)
                    {
                        // we swap the colors so reset when no search text is passed.
                        Swap(tb);
                        tb.Highlighted = true;
                    }
                    else
                    {
                        if (tb.Highlighted)
                        {
                            Swap(tb);
                            tb.Highlighted = false;
                        }
                    }
                }
            }
            else
            {
                int endIndex            = completeText.Length;
                int highlightStartIndex = completeText.IndexOf(searchText);

                completeText = tb.Text.Trim();

                tb.Inlines.Clear();
                if (highlightStartIndex >= 0)
                {
                    int highlightTextLength = searchText.Length;
                    int highlightEndIndex   = highlightStartIndex + highlightTextLength;

                    tb.Inlines.Add(completeText.Substring(0, highlightStartIndex));
                    tb.Inlines.Add(new Run()
                    {
                        Text       = completeText.Substring(highlightStartIndex, highlightTextLength),
                        Foreground = tb.HighlightForeground,
                        Background = tb.HighlightBackground
                    });
                    tb.Inlines.Add(completeText.Substring
                                       (highlightEndIndex,
                                       endIndex - highlightEndIndex));
                }
                else
                {
                    tb.Inlines.Add(completeText);
                }
            }
        }