根据TextWrapping财产获取TextBlock的线路? [英] Get the lines of the TextBlock according to the TextWrapping property?

查看:136
本文介绍了根据TextWrapping财产获取TextBlock的线路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF应用程序一个TextBlock。

I have a TextBlock in WPF application.

(文字,宽度,高度,TextWrapping,字号,粗细,的FontFamily)这个TextBlock的特性是动态的(进入在运行时的用户)。

The (Text, Width, Height, TextWrapping, FontSize, FontWeight, FontFamily) properties of this TextBlock is dynamic (entered by the user in the Runtime).

每一个用户的变化之一,以前的属性,TextBlock的内容到底在运行时改变时。 (每一件事情是确定这里)

every time the user change one the the previous properties, the end Content of the TextBlock is changed at the runtime. (every thing is ok here)

现在,我需要得到根据先前指定的属性是文本块的线路。
我的意思是我需要TextWrapping算法将导致该行。

Now I need to to get the lines of that "textblock" according to the previous specified properties. I mean I need the lines that TextWrapping algorithms will result.

在换句话说,我需要分割的字符串,每行或我需要用一个字符串花葶序列\\\

In other words, I need each line in separated string or I need a one string with Scape Sequence "\n".

任何想法要做到这一点??

Any Idea to do that ??

推荐答案

我会感到惊讶,如果有这样做的没有公开的方式(虽然没有人知道,尤其是与WPF)。的确看起来像 TextPointer类是我们的朋友,所以这里是一个基于的 TextBlock.ContentStart ,的 TextPointer.GetLineStartPosition 和的 TextPointer.GetOffsetToPosition

I would have been surprised if there is no public way of doing that (although one never knows, especially with WPF). And indeed looks like TextPointer class is our friend, so here is a solution based on the TextBlock.ContentStart, TextPointer.GetLineStartPosition and TextPointer.GetOffsetToPosition:

public static class TextUtils
{
    public static IEnumerable<string> GetLines(this TextBlock source)
    {
        var text = source.Text;
        int offset = 0;
        TextPointer lineStart = source.ContentStart.GetPositionAtOffset(1, LogicalDirection.Forward);
        do
        {
            TextPointer lineEnd = lineStart != null ? lineStart.GetLineStartPosition(1) : null;
            int length = lineEnd != null ? lineStart.GetOffsetToPosition(lineEnd) : text.Length - offset;
            yield return text.Substring(offset, length);
            offset += length;
            lineStart = lineEnd;
        }
        while (lineStart != null);
    }
}

有没有太多的解释这里 - 得到启动该行的位置,减去前行的起始位置,以获得行文本的长度,我们在这里。唯一棘手的(或无明显)部分是需要抵消 ContentStart 按之一,因为设计的 此属性总是返回TextPointer有其LogicalDirection设置为落后。 的,所以我们需要得到相同的(!?)的位置的指针,但是的 LogicalDirection设置为转发 的,所有的一切,这意味着: - )

There is not much to explain here - get the start position of the line, subtract the start position of the previous line to get the length of the line text and here we are. The only tricky (or non obvious) part is the need to offset the ContentStart by one since by design The TextPointer returned by this property always has its LogicalDirection set to Backward., so we need to get the pointer for the same(!?) position, but with LogicalDirection set to Forward, whatever all that means :-).

这篇关于根据TextWrapping财产获取TextBlock的线路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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