WPF的FlowDocument - 绝对字符位置 [英] WPF FlowDocument - Absolute Character Position

查看:592
本文介绍了WPF的FlowDocument - 绝对字符位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我打字一些文本,然后解析整个文本做处理在WPF的RichTextBox。在这个解析,我已经开始和每个单词的末尾的绝对字符位置。

I have a WPF RichTextBox that I am typing some text into and then parsing the whole of the text to do processing on. During this parse, I have the absolute character positions of the start and end of each word.

我想用这些字符位置格式设置应用到某些词。不过,我已经发现的FlowDocument使用TextPointer实例来标记文档中的位置。

I would like to use these character positions to apply formatting to certain words. However, I have discovered that the FlowDocument uses TextPointer instances to mark positions in the document.

我发现,我可以通过开始和结束指针构建它创建一个TextRange 。一旦我有我的TextRange可以很容易地应用格式内的文字。我一直在使用GetPositionAtOffset能为我的性格偏移的TextPointer让人怀疑它的偏移量是与我的不同,因为所选的文字是从我所期望的一个稍微不同的位置。

I have found that I can create a TextRange by constructing it with start and end pointers. Once I have the TextRange I can easily apply formatting to the text within it. I have been using GetPositionAtOffset to get a TextPointer for my character offset but suspect that its offset is different from mine because the selected text is in a slightly different position from what I expect.

我的问题是,我怎么能准确地转换绝对字符位置的TextPointer?

My question is, how can I accurately convert an absolute character position to a TextPointer?

推荐答案

我没有找到一个转换绝对字符位置到TextPosition实例的可靠途径。

I didn't find a reliable way of converting absolute character positions into TextPosition instances.

我的另一种解决方案是修改原来的解析,以个人的运行工作,而不是捕捉RichTextBox中的全部文本。与相对于一个特定的运行实例的字符位置的工作已被证明可靠的我。我认为移动我的心态更趋向的思考帮助的WPF方式。

My alternative solution was to modify the original parse to work on individual runs rather than capturing the whole text of the RichTextBox. Working with character positions that are relative to a specific Run instance has proved reliable for me. I think that moving my mindset more towards the WPF way of thinking has helped.

我把下面的方法在FlowDocument的导航运行(通过的http://blogs.msdn.com/prajakta /archive/2006/10/12/customize-richtextbox-to-allow-only-plain-text-input.aspx ):

I took the following approach for navigating runs in the FlowDocument (inspired by http://blogs.msdn.com/prajakta/archive/2006/10/12/customize-richtextbox-to-allow-only-plain-text-input.aspx):

// Get starting pointer
TextPointer navigator = flowDocument.ContentStart;

// While we are not at end of document
while (navigator.CompareTo(flowDocument.ContentEnd) < 0)
{
    // Get text pointer context
    TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);

    // Get parent as run
    Run run = navigator.Parent as Run;

    // If start of text element within run
    if (context == TextPointerContext.ElementStart && run != null)
    {
        // Get text of run
        string runText = run.Text;

        // ToDo: Parse run text
    }

    // Get next text pointer
    navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
}

这篇关于WPF的FlowDocument - 绝对字符位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆