Beispiel #1
0
        private List <TreeViewItemModel> GetTreeItem(FlowDocument document)
        {
            var list  = new List <TreeViewItemModel>();
            var start = document.ContentStart;

            while (start != null && document.ContentEnd.CompareTo(start) > -1)
            {
                var model   = new TreeViewItemModel();
                var context = start.GetPointerContext(LogicalDirection.Forward);
                //model.Header = context.ToString() + ":";
                switch (context)
                {
                case TextPointerContext.ElementStart:
                //    start = start.GetNextContextPosition(LogicalDirection.Forward);
                //    model.Items = GetTreeItem(document ,ref start);
                case TextPointerContext.None:
                case TextPointerContext.ElementEnd:
                //    return list;
                case TextPointerContext.EmbeddedElement:
                    model.Header = context.ToString() + ":";
                    list.Add(model);
                    break;

                case TextPointerContext.Text:
                    //default:
                    model.Header  = context.ToString() + ":";
                    model.Header += "length(" + start.GetTextRunLength(LogicalDirection.Forward) + ")" + start.GetTextInRun(LogicalDirection.Forward);
                    list.Add(model);
                    break;
                }
                start = start.GetNextContextPosition(LogicalDirection.Forward);
            }
            return(list);
        }
Beispiel #2
0
        void CheckWordsInFlowDocument(FlowDocument flowDocument) //do not hightlight keywords in this method
        {
            try
            {
                TextPointer navigator = flowDocument.ContentStart;
                var         text      = InputString;
                if (string.IsNullOrEmpty(text))
                {
                    return;
                }
                var   reg      = new Regex(RegexString);
                var   nameList = reg.GetGroupNames();
                Match match    = reg.Match(text);
                var   matchNum = 1;
                while (match.Success)
                {
                    TextPointer start = InputBox.Document.ContentStart;
                    string      word = match.Groups[0].ToString();
                    int         startPos = match.Index, endOffset = word.Length;

                    var model = new TreeViewItemModel()
                    {
                        Index = startPos, Header = $"match{matchNum},Index({startPos.ToString()})"
                    };
                    var groupNum = 0;
                    foreach (Capture g in match.Groups)
                    {
                        model.Items.Add(new TreeViewItemModel()
                        {
                            Header = $"group[{groupNum}]<{reg.GroupNameFromNumber(groupNum)}>: {g.Value}"
                        });
                        groupNum++;
                    }
                    dataList.Add(model);

                    Tag t = new Tag();
                    t.Word = word;
                    #region 设置高亮范围
                    t.StartPosition = GetPointer(flowDocument.ContentEnd, start, startPos);
                    t.EndPosition   = GetPointer(flowDocument.ContentEnd, t.StartPosition, endOffset);
                    m_tags.Add(t);
                    #endregion
                    match = match.NextMatch();
                    matchNum++;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }