首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用TextRange替换运行的字母?

如何使用TextRange替换运行的字母?
EN

Stack Overflow用户
提问于 2015-02-01 11:48:13
回答 1查看 724关注 0票数 0

我对FlowDocuments完全陌生,似乎在任何地方都找不到这个。

我创建了一个TextBlock,其中每个内联都是一个运行,只包含一个单词。(内联是用代码创建的)。然后,我使用TextRange替换运行中的一些字母。但是, TextRange似乎没有替换运行的字符,而是清空运行,并将字母追加到上一次运行。

(重要的是保留原始的TextBlock结构和原始的Run对象,因为我将事件绑定到它们。)

我做错了什么?TextRange.Text =文本实际上在做什么?

任何帮助都是非常感谢的。

示例

代码语言:javascript
复制
  ---Before textrange--
<Run>
6||perrla
</Run>
<Run>
1||                       **NOTE THIS IS A SPACE IN THE RUN
</Run>
<Run>
5||nc/at
</Run>

--After textrange --- 
<Run>           **NOTE THE EMPTY RUN
</Run>
<Run>
5|| test    **NOTE HOW TEST IS APPENDED TO THE SPACE OF THE PREVIOUS RUN
</Run>
<Run>
5||nc/at
</Run>

产生上述结果的代码:

代码语言:javascript
复制
            string text = nw.Replace("\0", string.Empty);

            TextPointer s = StartInlineElement.GetInsertionPosition(LogicalDirection.Forward);
            TextPointer start = s.GetPositionAtOffset(StartIndex);
            TextPointer end = start.GetPositionAtOffset(Text.Length);

            var y = start.GetAdjacentElement(LogicalDirection.Backward);
            if (y != null)
            {
                do
                {
                    GetXaml((TextElement)y);
                    y = (y as Run).PreviousInline;
                } while (y != null);
            }

            THIS IS WRONG -- IT EMPTIES THE RUN AND APPENDS TO THE RUN
            BEFORE IT.
            TextRange tr = new TextRange(start, end);
            tr.Text = text;

            y = start.GetAdjacentElement(LogicalDirection.Forward);
            if (y != null)
            {
                do
                {
                    GetXaml((TextElement)y);
                    y = (y as Run).PreviousInline;
                } while (y != null);
            }
        }

GetXaml来自微软(https://msdn.microsoft.com/en-us/library/system.windows.documents.textpointercontext(v=vs.110).aspx ),我轻描淡写地修改到:

代码语言:javascript
复制
     void GetXaml(TextElement element)
    {
        // Position a "navigator" pointer before the opening tag of the element.
        TextPointer navigator = element.ElementStart;

        while (navigator.CompareTo(element.ElementEnd) < 0)
        {
            switch (navigator.GetPointerContext(LogicalDirection.Forward))
            {
                case TextPointerContext.ElementStart:
                    // Output opening tag of a TextElement
                    Console.WriteLine(String.Format("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name));
                    break;
                case TextPointerContext.ElementEnd:
                    // Output closing tag of a TextElement
                    Console.WriteLine(String.Format("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name));
                    break;
                case TextPointerContext.EmbeddedElement:
                    // Output simple tag for embedded element
                    Console.WriteLine(String.Format("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name));
                    break;
                case TextPointerContext.Text:
                    // Output the text content of this text run
                    Console.WriteLine(String.Format("{0}||{1}", navigator.GetTextRunLength(LogicalDirection.Forward),
                        navigator.GetTextInRun(LogicalDirection.Forward)));
                    break;
            }

            // Advance the naviagtor to the next context position.
            navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);

        } 
    } 
EN

回答 1

Stack Overflow用户

发布于 2015-04-10 17:55:57

这似乎是因为TextRange.Start总是有一个LogicalDirection或向后。因此,它将文本放在前面的元素中。

也许我对我的特殊情况的修正可以帮助你解决你自己的问题:

代码语言:javascript
复制
if (this.Selection.Start.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart &&
    this.Selection.End.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd &&
    this.Selection.Start.Parent == this.Selection.End.Parent && this.Selection.Start.Parent is Run)
{
     ((Run)this.Selection.Start.Parent).Text = "NewText";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28262339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档