Exemple #1
0
 private static void OnHtContentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs arg)
 {
     if (sender is HighlightTextBlock)
     {
         HighlightTextBlock ctrl    = sender as HighlightTextBlock;
         HighlightContent   content = ctrl.HlContent;
         ctrl.Inlines.Clear();
         if (content != null)
         {
             ctrl.Inlines.AddRange(MakeRunsFromContent(content));
         }
     }
 }
Exemple #2
0
        private static IEnumerable <Run> MakeRunsFromContent(HighlightContent content)
        {
            List <Run> listRtn = new List <Run>();

            if (!string.IsNullOrWhiteSpace(content.Content))
            {
                if (string.IsNullOrWhiteSpace(HighlightContent.ToHighlight))
                {
                    listRtn.Add(new Run(content.Content));
                }
                else
                {
                    string strContent = content.Content.Trim();
                    string strToHl    = HighlightContent.ToHighlight.Trim();

                    if (HighlightContent.Mode == HighlightContentMode.FullMatch)
                    {
                        int iCurrIndex = 0;
                        while (true)
                        {
                            int iFoundIndex = strContent.IndexOf(strToHl, iCurrIndex, StringComparison.OrdinalIgnoreCase);
                            if (iFoundIndex != -1)
                            {
                                if (iFoundIndex > iCurrIndex)
                                {
                                    listRtn.Add(new Run(strContent.Substring(iCurrIndex, iFoundIndex - iCurrIndex)));
                                }
                                listRtn.Add(new Run(strContent.Substring(iFoundIndex, strToHl.Length))
                                {
                                    Background = Brushes.Yellow
                                });
                            }
                            else
                            {
                                if (iCurrIndex < strContent.Length)
                                {
                                    listRtn.Add(new Run(strContent.Substring(iCurrIndex, strContent.Length - iCurrIndex)));
                                }
                                break;
                            }

                            iCurrIndex = iFoundIndex + strToHl.Length;
                        }
                    }
                    else
                    {
                        int      iCurrIndex = 0;
                        string[] keys       = strToHl.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (keys.Length == 0)
                        {
                            listRtn.Add(new Run(content.Content));
                        }
                        else
                        {
                            var keysToAnalysis = keys.Select(k => new KeyItem {
                                Key = k, Finished = false
                            });
                            while (true)
                            {
                                int iFoundIndex     = strContent.Length;
                                int iFoundKeyLength = 0;
                                foreach (var key in keysToAnalysis)
                                {
                                    if (!key.Finished)
                                    {
                                        int iThisFoundIndex = strContent.IndexOf(key.Key, iCurrIndex, StringComparison.OrdinalIgnoreCase);
                                        if (iThisFoundIndex == -1)
                                        {
                                            key.Finished = true;
                                        }
                                        else
                                        {
                                            if (iThisFoundIndex < iFoundIndex)
                                            {
                                                iFoundIndex     = iThisFoundIndex;
                                                iFoundKeyLength = key.Key.Length;
                                            }
                                        }
                                    }
                                }

                                if (iFoundIndex == strContent.Length)
                                {
                                    listRtn.Add(new Run(strContent.Substring(iCurrIndex, strContent.Length - iCurrIndex)));
                                    break;
                                }
                                else
                                {
                                    if (iFoundIndex > iCurrIndex)
                                    {
                                        listRtn.Add(new Run(strContent.Substring(iCurrIndex, iFoundIndex - iCurrIndex)));
                                    }
                                    listRtn.Add(new Run(strContent.Substring(iFoundIndex, iFoundKeyLength))
                                    {
                                        Background = Brushes.Yellow
                                    });
                                }

                                iCurrIndex = iFoundIndex + iFoundKeyLength;
                            }
                        }
                    }
                }
            }

            return(listRtn);
        }