public bool IsMatchForSearchTerm(string searchTerm)
 {
     return(PackageSearchUtil.ForTerm(searchTerm).IsMatch(this));
 }
Example #2
0
        private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Label     lbl   = d as Label;
            TextBlock block = d as TextBlock;

            if (lbl != null)
            {
                block = lbl.Content as TextBlock;

                if (block == null)
                {
                    string    lblChild = lbl.Content as string;
                    TextBlock newChild = new TextBlock {
                        Text = lblChild ?? ""
                    };
                    lbl.Content = newChild;
                    block       = newChild;
                }
            }

            if (block == null)
            {
                return;
            }

            string searchText = GetHighlightText(d);
            string blockText  = GetSourceText(d);

            if (blockText == null)
            {
                return;
            }

            int last = 0;

            block.Inlines.Clear();

            if (!string.IsNullOrEmpty(searchText))
            {
                IReadOnlyList <PackageSearchUtil.Range> matches = PackageSearchUtil.ForTerm(searchText).GetMatchesInText(blockText);

                for (int i = 0; i < matches.Count; ++i)
                {
                    if (matches[i].Length == 0)
                    {
                        continue;
                    }

                    if (last < matches[i].Start)
                    {
                        string inserted = blockText.Substring(last, matches[i].Start - last);
                        block.Inlines.Add(inserted);
                        last += inserted.Length;
                    }

                    Run highlight = new Run(matches[i].ToString());
                    highlight.SetBinding(FrameworkContentElement.StyleProperty, new Binding
                    {
                        Mode   = BindingMode.OneWay,
                        Source = d,
                        Path   = new PropertyPath(HighlightStyleProperty)
                    });
                    block.Inlines.Add(highlight);
                    last += matches[i].Length;
                }
            }

            if (last < blockText.Length)
            {
                block.Inlines.Add(blockText.Substring(last));
            }
        }